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

Fixed TmTheme scope issue for monaco-editor #9

Merged
merged 1 commit into from Aug 5, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/index.ts
@@ -1,5 +1,6 @@
import { Registry, StackElement, INITIAL } from 'monaco-textmate'
import * as monacoNsps from 'monaco-editor'
import { TMToMonacoToken } from './tm-to-monaco-token';

class TokenizerState implements monacoNsps.languages.IState {

Expand Down Expand Up @@ -34,7 +35,7 @@ class TokenizerState implements monacoNsps.languages.IState {
* @param registry TmGrammar `Registry` this wiring should rely on to provide the grammars
* @param languages `Map` of language ids (string) to TM names (string)
*/
export function wireTmGrammars(monaco: typeof monacoNsps, registry: Registry, languages: Map<string, string>) {
export function wireTmGrammars(monaco: typeof monacoNsps, registry: Registry, languages: Map<string, string>, editor?: monacoNsps.editor.ICodeEditor) {
return Promise.all(
Array.from(languages.keys())
.map(async (languageId) => {
Expand All @@ -48,7 +49,7 @@ export function wireTmGrammars(monaco: typeof monacoNsps, registry: Registry, la
tokens: res.tokens.map(token => ({
...token,
// TODO: At the moment, monaco-editor doesn't seem to accept array of scopes
scopes: token.scopes[token.scopes.length - 1]
scopes: editor ? TMToMonacoToken(editor, token.scopes) : token.scopes[token.scopes.length - 1]
})),
}
}
Expand Down
53 changes: 53 additions & 0 deletions src/tm-to-monaco-token.ts
@@ -0,0 +1,53 @@
import * as monacoNsps from 'monaco-editor'

// as described in issue: https://github.com/NeekSandhu/monaco-textmate/issues/5
export const TMToMonacoToken = (editor: monacoNsps.editor.ICodeEditor, scopes: string[]) => {
let scopeName = "";
// get the scope name. Example: cpp , java, haskell
for (let i = scopes[0].length - 1; i >= 0; i -= 1) {
const char = scopes[0][i];
if (char === ".") {
break;
}
scopeName = char + scopeName;
}

// iterate through all scopes from last to first
for (let i = scopes.length - 1; i >= 0; i -= 1) {
const scope = scopes[i];

/**
* Try all possible tokens from high specific token to low specific token
*
* Example:
* 0 meta.function.definition.parameters.cpp
* 1 meta.function.definition.parameters
*
* 2 meta.function.definition.cpp
* 3 meta.function.definition
*
* 4 meta.function.cpp
* 5 meta.function
*
* 6 meta.cpp
* 7 meta
*/
for (let i = scope.length - 1; i >= 0; i -= 1) {
const char = scope[i];
if (char === ".") {
const token = scope.slice(0, i);
if (
editor['_themeService'].getTheme()._tokenTheme._match(token + "." + scopeName)._foreground >
1
) {
return token + "." + scopeName;
}
if (editor['_themeService'].getTheme()._tokenTheme._match(token)._foreground > 1) {
return token;
}
}
}
}

return "";
};