Sublime Forum

[SOLVED] Put Line-Highlight after found text

#1

Hello,
I try to create a plugin for detect Vietnamese in html file. I have a problem: The highlight just fill 1 letter each words
Like this: https://gyazo.com/6916576a15412f950c9418bed68a6378
I want to show like this: https://gyazo.com/fc13fe7f40888d421b78d9ff9447bc4d
Please help me fix it.
Here is my code: (Sorry my english is not good)

import sublime
import sublime_plugin
class HighlightChars(sublime_plugin.EventListener): 
  def on_activated(self, view):
    self.view = view
    self.runPlugin()
  def on_modified_async(self, view):
    self.runPlugin()
  def runPlugin(self):
    if ( (self.view.file_name() is not None) and (self.view.file_name().find('.html') != -1 or self.view.file_name().find('.css') != -1 or self.view.file_name().find('.scss') != -1 or self.view.file_name().find('.js') != -1 or self.view.file_name().find('.pug') != -1)): 
      self.highlight()
  def highlight(self):
    needle = '[àáãạảăắằẳẵặâấầẩẫậèéẹẻẽêềếểễệđìíĩỉịòóõọỏôốồổỗộơớờởỡợùúũụủưứừửữựỳỵỷỹýÀÁÃẠẢĂẮẰẲẴẶÂẤẦẨẪẬÈÉẸẺẼÊỀẾỂỄỆĐÌÍĨỈỊÒÓÕỌỎÔỐỒỔỖỘƠỚỜỞỠỢÙÚŨỤỦƯỨỪỬỮỰỲỴỶỸÝ]'
    self.view.add_regions('foo_string', self.view.find_all(needle), 'string2', 'circle')
0 Likes

#2

Your current code is just finding all the unicode characters present in that array like string (needle) and then adding a region for it. Your code is not doing anything to highlight (or add regions) to the line itself that contains those unicode characters.

You can use the View.line method to get the region encompassing the line containing the region or point identified by the Vietnamese characters and then add another region to highlight that. Something like the following.

import sublime
import sublime_plugin

class HighlightChars(sublime_plugin.EventListener):

    def on_activated(self, view):
        self.view = view
        self.runPlugin()

    def on_modified_async(self, view):
        self.runPlugin()

    def runPlugin(self):
        if ( (self.view.file_name() is not None) and (self.view.file_name().find('.html') != -1 or self.view.file_name().find('.css') != -1 or self.view.file_name().find('.scss') != -1 or self.view.file_name().find('.js') != -1 or self.view.file_name().find('.pug') != -1)):
            self.highlight()

    def highlight(self):
        needle = '[àáãạảăắằẳẵặâấầẩẫậèéẹẻẽêềếểễệđìíĩỉịòóõọỏôốồổỗộơớờởỡợùúũụủưứừửữựỳỵỷỹýÀÁÃẠẢĂẮẰẲẴẶÂẤẦẨẪẬÈÉẸẺẼÊỀẾỂỄỆĐÌÍĨỈỊÒÓÕỌỎÔỐỒỔỖỘƠỚỜỞỠỢÙÚŨỤỦƯỨỪỬỮỰỲỴỶỸÝ]'
        line_regions = []
        for region in self.view.find_all(needle):
            line_region = self.view.line(region)
            line_regions.append(line_region)

        self.view.add_regions("line_string", line_regions, 'region.greenish')
        self.view.add_regions('foo_string', self.view.find_all(needle), 'region.pinkish', 'circle')

Note that you have used string2 in your sample code, which I assume represent an object in your color scheme with a color. I have used the generic ish colors (region.greenish, region.pinkish etc) as scopes to color the regions.

Hope this helps !

1 Like

#3

You saved my life. Thank you! I understood.

0 Likes