Changing the syntax highlighting

As you may remember from the previous chapter, syntax highlighting is defined in a .yaml file stored in a folder named languages. We want to allow the user to easily define their own syntax highlighting keywords and schemes by simply creating another .yaml file.

To experiment with this feature, create yourself a .yaml file in the languages folder. Ensure it contains numbers, strings, and any other category of keywords you like.

If you want to, you can copy this small example for SQL:

categories:
keywords:
color: orange
matches: [select, where, and, from, order, by, group]

dangerous:
color: red4
matches: [set, update, drop, replace]

numbers:
color: purple

strings:
color: red

Now that we have another syntax highlighting file to test with we can write a method to load it:

def load_syntax_highlighting_file(self):
syntax_file = filedialog.askopenfilename(filetypes=[("YAML file",
("*.yaml", "*.yml"))])
if syntax_file:
self.highlighter.clear_highlight()
self.highlighter = Highlighter(self.text_area, syntax_file)
self.highlighter.force_highlight()

Once again we need the path to the new .yaml file. We get this from the user with the askopenfilename function. This time we want to ensure that the user only chooses a .yaml file, so we let them know by using the filetypes keyword argument.

The filetypes argument needs a list of available file types. This list should contain a tuple for each type. This tuple should contain a string to tell the user what the file type should be, followed by a string (or tuple of strings if there are more than one), which indicates the file extension of any allowed files.

For our example, we want to enable any .yaml file, which could be indicated by a .yml or .yaml file extension. The strings to represent these are "*.yml" and "*.yaml".  We then signal the file types to the user with the string "YAML file". We combine these two arguments into a tuple of ("YAML file", ("*.yaml", "*.yml")). Since the only file type we want to allow is .yaml files, our list will only contain this one item.

This function will now show the usual GUI window but will only allow the user to select files that end with .yml or .yaml.

If the user does not cancel the operation, we need to create a new instance of our Highlighter class, which uses the selected .yaml file.

We first clear the current highlighting from our TextArea, create a new instance of the Highlighter class with the chosen file path, then re-highlight the TextArea's contents with the new scheme.

Now that we have this method available we can add it to our Tools menu by wrapping it in another function:

def tools_change_syntax_highlighting(self, event=None):
"""
Ctrl+M
"""
self.load_syntax_highlighting_file()

The last thing to address is the clear_highlight method in our Highlighter class:

def clear_highlight(self):
for category in self.categories:
self.text_widget.tag_remove(category, 1.0, tk.END)

In this method we just loop through each category for our keywords and remove the associated tags. We can leave the numbers and strings since they will be used by any other syntax file, and the tags will be reconfigured to use the new colors when our new Highlighter instance is created.

With that added, we can now switch syntax to a different language. Run your editor and type some code in the language that you created a .yaml file for. Now open the tools menu and select Change Syntax Highlighting and open your .yaml file. You should see the keywords of your language change color.

Now that this feature has been added our editor is no longer strictly a Python editor. If you prefer another language, you can change the .yaml file opened in the __init__ method of your texteditor.py file to point to that language instead.

Now that the user has some control over what language they wish to work in, we should also give them some choices over how the editor looks. Let's add the ability for them to change the font of their editor, then afterwards we can allow them to set their own colors, too.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset