ShaderEditor icon indicating copy to clipboard operation
ShaderEditor copied to clipboard

Feature Request: Lexer-/Parser-Based Syntax highlighting

Open AntonPieper opened this issue 2 years ago • 3 comments

I think migrating to treesitter (Maybe using a modified Java implementation? Would need to modify the Languages class and build.py to support GLSL though) based syntax highlighting could be worthwhile.

Reasons:

  1. It is fast for big files
  2. It potentially supports changes in source code without recreating all the spans
  3. Potential for future semantic highlighting / analysis for autocompletion

AntonPieper avatar Jun 14 '23 07:06 AntonPieper

Treesitter looks really interesting! I'll give it a try for sure! Since fragment shaders are usually quite short (of course I know this is not always the case 😉), the current solution is/was good enough for most cases. But it sure has it's shortcomings, and that the editor is getting slower when the code gets bigger has plagued this app for while now.

markusfisch avatar Jun 15 '23 11:06 markusfisch

The Java implementation is flawed IMO, but there also is android-tree-sitter, which is a simple dependency for Gradle. This is already used in their IDE AndroidIDE to highlight XML and Java. They (obviously) don't have GLSL grammar, but this might be a simple task as the developer kindly outlined the steps in an issue I opened. Having tried a simple example using the Java grammar (com.itsaky.androidide:tree-sitter-java:3.0.1), it seems to "just work". There also already is an official (at least linked on the official site) grammar for GLSL: theHamsta/tree-sitter-glsl.

AntonPieper avatar Jun 16 '23 10:06 AntonPieper

Hello again!

I think I have now implemented a syntax highlighting solution that works quite fast. The lag now comes from the EditText and not the (separate) SyntaxEditor View. I have also moved the line numbers into its own view, however this has one side effect: when the EditText changes size vertically (i.e. a new line), the phone scrolls to put the cursor at the far left of the screen. I will look into MovementMethod to see if this can be fixed. Maybe this is not that big of a problem? Also I have increased the API level temporarily to use android/trace, this will be reverted. Of course I will open several pull requests for the various changes. You can look at the changed version here: c-highlight branch

AntonPieper avatar Jul 17 '23 19:07 AntonPieper