Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Highlight TableViewer cells with TM4E #160

Closed
PhaserEditor2D opened this issue Jun 25, 2018 · 12 comments
Closed

Highlight TableViewer cells with TM4E #160

PhaserEditor2D opened this issue Jun 25, 2018 · 12 comments

Comments

@PhaserEditor2D
Copy link
Contributor

Hi,

I need to create a tm4e JavaScript tokenizer to render the content of a TableViewer cell.

My tool uses Eclipse BlueSky (from @mickaelistria ) and the idea is to get the current TM theme associated with the JavaScript grammar, create a tokenizer and transform the result in an array of JFace StyleRange, then I can "syntax paint" the cell.

Any tip is welcome!

I attach here an image of the table:

image

@mickaelistria
Copy link
Contributor

This is a very interesting and not so easy question ;)

Have you seen some occurences of other languages (like Java with JDT) which managed to reuse the syntax highlighting in other widgets than StyledText? If so, it could simply be a matter of mimicking their implementation, using the TMPresentationReconciler instead.
But to be honest, I'm not sure anyone ever did that. If I see something of that kind, I'll add a note here.

Another possibility is simply to find the right code from tm4e to add into the LabelProvider. It might be simple enough, but there are some risks of this taking too much time in a synchronous UI Thread operation, then causing freezes in the IDE.

@PhaserEditor2D
Copy link
Contributor Author

I just did it! :)

image

I can go home now. Tomorrow I will work more on this. This is the important code:


private StyleRange[] parseCode(String line) {
	List<StyleRange> ranges = new ArrayList<>();

	ITokenizeLineResult result = _grammar.tokenizeLine(line);
	for (IToken token : result.getTokens()) {
		for (String scope : token.getScopes()) {
			org.eclipse.jface.text.rules.IToken themeToken = _theme.getToken(scope);
			if (themeToken != null) {
				Object data = themeToken.getData();
				if (data != null && data instanceof TextAttribute) {
					TextAttribute attr = (TextAttribute) data;
					if (attr.getForeground() != null || attr.getBackground() != null) {
						StyleRange range = new StyleRange(token.getStartIndex(),
								token.getEndIndex() - token.getStartIndex(), attr.getForeground(),
								attr.getBackground());
						ranges.add(range);
					}
				}
			}
		}
	}

	return ranges.toArray(new StyleRange[ranges.size()]);
}


@PhaserEditor2D
Copy link
Contributor Author

Ok, I close this. Now it even updates when the user changes the theme.

but there are some risks of this taking too much time in a synchronous UI Thread operation, then causing freezes in the IDE.

I guess the time-consuming part is the parsing, but I will do this in the "search thread" and not in the UI thread.

Thanks!

image

@mickaelistria
Copy link
Contributor

mickaelistria commented Jun 26, 2018 via email

@angelozerr
Copy link
Contributor

Sorry @PhaserEditor2D I was very busy. I have seen that you find a solution.

It might be simple enough, but there are some risks of this taking too much time in a synchronous UI Thread operation, then causing freezes in the IDE.

Parsing one line, I think it's quick.

I can go home now. Tomorrow I will work more on this. This is the important code:

At first you can find this similar code on Markdown support https://github.com/eclipse/tm4e/blob/master/org.eclipse.tm4e.markdown/src/main/java/org/eclipse/tm4e/markdown/TMHTMLRenderer.java (I'm using it in typescript.java for hover typescript JSDoc which can use Markdown)

My fear is that I would like to support like vscode-textmate #38 and it will change the API, but it's a very hard task... But it will open the door on a lot of feature like get quickly token in an offset (required for language configuration like #159 and #141), highlighjt only visble view port, manage an hover (in debug mode kind) to see token info, etc...

@PhaserEditor2D
Copy link
Contributor Author

Hi @angelozerr

Thanks for the answer. I will take a look to the HTML renderer, I need something like that to render the Phaser docs and in the examples of code, that is a local website.

All the changes you are planning to do are very welcome. In Phaser Editor we need to get token information because we show some fancy "text hover" with animations, images, and things like that. Actually, I plan to use Code Mining to show images next to the images keys.

@PhaserEditor2D
Copy link
Contributor Author

Now we use TM4E to syntax coloring of the Phaser API docs:

image

At a certain point, we should implement it also for LSP4E docs.

@angelozerr
Copy link
Contributor

angelozerr commented Feb 9, 2019 via email

@PhaserEditor2D
Copy link
Contributor Author

I should check TS IDE.

@angelozerr
Copy link
Contributor

angelozerr commented Feb 9, 2019 via email

@mickaelistria
Copy link
Contributor

mickaelistria commented Feb 9, 2019 via email

@PhaserEditor2D
Copy link
Contributor Author

If LSP4E uses mylyn for markdown then maybe the better is to implement syntax coloring i the mylyn project. Markdown has this format to say that a block is of certain language, so mylyn can parse it and color it, or maybe, create an extension point to load a third-party highlighter. So custom products can register highlighters based on TM4E or any other. In my case, I like to use the same TM4E theme for editors and documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants