The idea of ​​highlighting text, completing code, etc. In programming

I want to know the idea of ​​advanced features of text editors such as text selection, code completion, automatic indentation, etc.

To make my idea clear, I believe that text selection reads all the text in a line, and then replaces the regular expression of keywords with keywords + color codes and replaces the text again. It looks logical, but it would be so inefficient to do it with every keystroke when your file is 4000 lines, for example! So I want to know the idea of ​​implementing such a thing in C #, for example (any other language would be great, but what I'm experimenting right now).

+4
source share
3 answers

Syntax highlighting: It comes to mind. I have not actually tried this example, so I can’t say anything about performance, but this is perhaps the easiest way to get the basic syntax highlighted.

Auto-completion: Given a list of possible keywords (which can be filtered based on context), you can quickly drop everything that does not match what the user is currently typing. In most languages, you can safely limit yourself to one “word,” since spaces are usually not legal in an identifier. For example, if I start typing “li,” the autocomplete database may drop everything that doesn't start with the letters “l” and “i” (ignoring the case). As the user continues to type, more and more options may be discarded until only one or at least a few remain. Since you just look at one word at a time, that would be really fast.

Indent: A quick and dirty approach that (sort of) works in C-type languages ​​should have a counter that you increment once for each "{" and decrement once for each "}. When you press enter to start a new line , then the indentation level is counter * indentWidth , where indentWidth is the constant number of spaces or tabs for the indentation, but this has a serious drawback - consider the following:

 if(foo) bar(); // This line should be indented, but how does the computer know? 

To handle this, you can look for lines that end with the ')' character, rather than a semicolon.

+1
source

Old, but still usable resource for internal elements editor - of The Craft of the Editing the Text . Chapter 7 directly addresses the issue of reissuing strategies.

+1
source

To make "advanced" syntax highlighting - that is, highlighting that requires contextual knowledge, often requires a parser. Most parsers are built on some kind of formal grammar that exists in different ways: LL , LALR and LR are common.

However, most parsers work with whole files, which is rather ineffective for editing text, so instead we turn to incremental parsers . Incremental parsers use the knowledge of the language and the structure of what was previously processed to restore the minimum amount of work.

Here are some links to incremental parsing:

+1
source

All Articles