diff options
| author | DanielGavin <danielgavin5@hotmail.com> | 2024-05-02 23:52:10 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-05-02 23:52:10 +0200 |
| commit | 3fc35ca009a2569e6cee46b3c2ff90c10e56d237 (patch) | |
| tree | 068a4a123e663fdcd2fe70a4bbaaa62af2692648 | |
| parent | db1941ff2fe38dea52ad522566c75fc861ddabc1 (diff) | |
| parent | 1005e465f8c70551699abd9e00ab28f676f928e8 (diff) | |
Merge pull request #373 from thetarnav/semantic-tokens-import-namespace
Highlight import namespace using semantic tokens
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | .vscode/launch.json | 10 | ||||
| -rw-r--r-- | editors/vscode/syntaxes/odin.tmLanguage.json | 8 | ||||
| -rw-r--r-- | src/server/semantic_tokens.odin | 52 |
4 files changed, 57 insertions, 15 deletions
@@ -14,4 +14,4 @@ /ols /odinfmt ols.json - +.vscode/settings.json diff --git a/.vscode/launch.json b/.vscode/launch.json index e2ecda4..bf5a6b5 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -9,12 +9,18 @@ "request": "attach", "name": "Attach OLS", "processId": "${command:pickProcess}" - } + }, { "type": "cppvsdbg", "request": "launch", "name": "Run unit", "program": "C:\\Users\\Daniel\\Desktop\\Computer_Science\\ols\\test_unit.exe", - } + }, + { + "type": "lldb", + "request": "attach", + "name": "Attach lldb", + "pid": "${command:pickProcess}" + }, ] }
\ No newline at end of file diff --git a/editors/vscode/syntaxes/odin.tmLanguage.json b/editors/vscode/syntaxes/odin.tmLanguage.json index 48f9687..3434596 100644 --- a/editors/vscode/syntaxes/odin.tmLanguage.json +++ b/editors/vscode/syntaxes/odin.tmLanguage.json @@ -256,14 +256,6 @@ "beginCaptures": {"0": {"name": "keyword.control.import.odin"}}, "end": "(?=^|;)", "patterns": [ - { "name": "string.import.odin", - "match": "([\"`])([\\w\\.]*[/:])*([A-Za-z_]\\w*)([\"`])", - "captures": { - "1": {"name": "punctuation.definition.string.begin.odin"}, - "3": {"name": "entity.name.namespace.odin"}, - "4": {"name": "punctuation.definition.string.end.odin"} - } - }, { "name": "entity.name.alias.odin", "begin": "\\b[A-Za-z_]\\w*", "beginCaptures": {"0": {"name": "entity.name.namespace.odin"}}, diff --git a/src/server/semantic_tokens.odin b/src/server/semantic_tokens.odin index d2240f8..bddff1c 100644 --- a/src/server/semantic_tokens.odin +++ b/src/server/semantic_tokens.odin @@ -9,6 +9,7 @@ package server import "core:fmt" import "core:log" +import "core:unicode/utf8" import "core:odin/ast" import "core:odin/tokenizer" @@ -353,10 +354,7 @@ visit_node :: proc(node: ^ast.Node, builder: ^SemanticTokenBuilder) { case ^Defer_Stmt: visit_node(n.stmt, builder) case ^Import_Decl: - if n.name.text != "" { - write_semantic_token(builder, n.name, .Namespace) - } - + visit_import_decl(n, builder) case ^Or_Return_Expr: visit_node(n.expr, builder) case ^Or_Else_Expr: @@ -501,6 +499,52 @@ visit_selector :: proc(selector: ^ast.Selector_Expr, builder: ^SemanticTokenBuil visit_ident(selector.field, selector, {}, builder) } +visit_import_decl :: proc(decl: ^ast.Import_Decl, builder: ^SemanticTokenBuilder) { + /* + hightlight the namespace in the import declaration + + import "pkg" + ^^^ + import "core:fmt" + ^^^ + import "core:odin/ast" + ^^^ + import foo "core:fmt" + ^^^ + */ + + if decl.name.text != "" { + write_semantic_token(builder, decl.name, .Namespace) + } + else if len(decl.relpath.text) > 2 { + + end := len(decl.relpath.text) - 1 + pos := end + + for { + if pos > 1 { + ch, w := utf8.decode_last_rune_in_string(decl.relpath.text[:pos]) + + switch ch { + case ':', '/': // break + case: + pos -= w + continue + } + } + + break + } + + write_semantic_at_pos( + builder, + decl.relpath.pos.offset+pos, + end-pos, + .Namespace, + ) + } +} + visit_ident :: proc( ident: ^ast.Ident, symbol_ptr: rawptr, |