-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add semantic token highlighting
- Loading branch information
Showing
4 changed files
with
43 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
import {integer, SemanticTokensBuilder} from 'vscode-languageserver'; | ||
import {SemanticTokensBuilder} from 'vscode-languageserver'; | ||
import {DocumentManagerInterface, ParseResult} from './document_manager'; | ||
import {collectIdentTerminals, collectMatchingTerminals} from "./parseTree/collect_terminals"; | ||
import {definitionSymbolsEqual, getDefinitionSymbolFromMatch} from "./go_to_definition"; | ||
import {collectIdentTerminals} from "./parseTree/collect_terminals"; | ||
import {getDefinitionSymbolFromMatch} from "./go_to_definition"; | ||
import {nonNullable} from "./utils/array"; | ||
import {notNull} from "antlr4ts/misc"; | ||
import {DeclarationType} from "./tasks/create_symbol_table"; | ||
|
||
export const tokenModifier = ['declaration', 'deprecated']; | ||
export const tokenTypes = ['function', 'variable', 'parameter', 'event']; | ||
export const tokenModifier = []; | ||
export const tokenTypes = ['function', 'variable', 'parameter']; | ||
|
||
export const getSemanticTokens = async (parseResult: ParseResult, documentManager: DocumentManagerInterface) => { | ||
// collect references | ||
const candidateTerminals = collectIdentTerminals(parseResult.parserTree); | ||
const terminalDefinitions = candidateTerminals | ||
.map(async terminal => getDefinitionSymbolFromMatch(parseResult, terminal, documentManager)); | ||
|
||
const tokensBuilder = new SemanticTokensBuilder(); | ||
const tokensBuilder = new SemanticTokensBuilder(); | ||
(await Promise.all(terminalDefinitions)) | ||
.filter(nonNullable) | ||
.filter(nonNullable) | ||
.forEach(terminalDefinition => { | ||
const tokenType = getTokenType(terminalDefinition.symbol.declaration) | ||
if (tokenType) { | ||
const line = terminalDefinition.origin.match.symbol.line - 1; | ||
const char = terminalDefinition.origin.match.symbol.charPositionInLine; | ||
const length = terminalDefinition.origin.match.symbol.stopIndex - terminalDefinition.origin.match.symbol.startIndex + 1; | ||
tokensBuilder.push(line, char, length, tokenType, [] as any) | ||
} | ||
const tokenType = getTokenType(terminalDefinition.symbol.declaration); | ||
if (tokenType) { | ||
const line = terminalDefinition.origin.match.symbol.line - 1; | ||
const char = terminalDefinition.origin.match.symbol.charPositionInLine; | ||
const length = terminalDefinition.origin.match.symbol.stopIndex - terminalDefinition.origin.match.symbol.startIndex + 1; | ||
tokensBuilder.push(line, char, length, tokenType, [] as any); | ||
} | ||
}); | ||
|
||
return tokensBuilder.build(); | ||
}; | ||
|
||
const getTokenType = (declarationType: DeclarationType): number|undefined => { | ||
switch (declarationType) { | ||
case DeclarationType.Channel: | ||
case DeclarationType.Free: | ||
case DeclarationType.Const: | ||
return tokenTypes.indexOf('variable') | ||
} | ||
const getTokenType = (declarationType: DeclarationType): number | undefined => { | ||
switch (declarationType) { | ||
case DeclarationType.Channel: | ||
case DeclarationType.Free: | ||
case DeclarationType.Const: | ||
case DeclarationType.Variable: | ||
return tokenTypes.indexOf('variable'); | ||
case DeclarationType.Fun: | ||
case DeclarationType.LetFun: | ||
case DeclarationType.Event: | ||
return tokenTypes.indexOf('function'); | ||
case DeclarationType.Parameter: | ||
case DeclarationType.DefineParameter: | ||
case DeclarationType.ExpandParameter: | ||
return tokenTypes.indexOf('parameter'); | ||
} | ||
|
||
return undefined | ||
} | ||
return undefined; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters