diff --git a/server/src/e2e/tolk/testcases/completion/local.test b/server/src/e2e/tolk/testcases/completion/local.test index 282aab58..8826e6ab 100644 --- a/server/src/e2e/tolk/testcases/completion/local.test +++ b/server/src/e2e/tolk/testcases/completion/local.test @@ -33,7 +33,7 @@ fun foo() { } } ------------------------------------------------------------------------ -5 error any +5 error int ======================================================================== Variables completion from catch clause @@ -45,8 +45,8 @@ fun foo() { } } ------------------------------------------------------------------------ -5 error any -5 errorData any +5 error int +5 errorData unknown ======================================================================== Variables completion from match expression diff --git a/server/src/e2e/tolk/testcases/completion/snippets.test b/server/src/e2e/tolk/testcases/completion/snippets.test new file mode 100644 index 00000000..983b0e3f --- /dev/null +++ b/server/src/e2e/tolk/testcases/completion/snippets.test @@ -0,0 +1,30 @@ +======================================================================== +Catch snippet +======================================================================== +fun test() { + try { + // ... + } cat +} +------------------------------------------------------------------------ +14 catch + +======================================================================== +Catch snippet as expression +======================================================================== +fun test() { + cat +} +------------------------------------------------------------------------ +No completion items + +======================================================================== +No completion for catch variable name +======================================================================== +const eeee = 100 + +fun test() { + try {} catch (e) {} +} +------------------------------------------------------------------------ +No completion items diff --git a/server/src/languages/tolk/completion/CompletionContext.ts b/server/src/languages/tolk/completion/CompletionContext.ts index 4f3df947..4809bc59 100644 --- a/server/src/languages/tolk/completion/CompletionContext.ts +++ b/server/src/languages/tolk/completion/CompletionContext.ts @@ -20,6 +20,7 @@ export class CompletionContext { public insideImport: boolean = false public isAnnotationName: boolean = false public expectMatchArm: boolean = false + public catchVariable: boolean = false // struct fields public inNameOfFieldInit: boolean = false @@ -54,6 +55,10 @@ export class CompletionContext { this.isAnnotationName = true } + if (parent.type === "catch_clause" && element.node.type === "identifier") { + this.catchVariable = true + } + if (parent.type === "binary_operator" && parent.parent?.type === "match_arm") { // match (a) { // @@ -127,6 +132,7 @@ export class CompletionContext { !this.insideImport && !this.structTopLevel && !this.expectMatchArm && + !this.catchVariable && !this.isAnnotationName ) } diff --git a/server/src/languages/tolk/completion/providers/ReferenceCompletionProvider.ts b/server/src/languages/tolk/completion/providers/ReferenceCompletionProvider.ts index 8fcff3f2..b70a344f 100644 --- a/server/src/languages/tolk/completion/providers/ReferenceCompletionProvider.ts +++ b/server/src/languages/tolk/completion/providers/ReferenceCompletionProvider.ts @@ -24,6 +24,7 @@ export class ReferenceCompletionProvider implements CompletionProvider + const prevSibling = ctx.element.node.parent?.previousSibling + if (prevSibling?.firstChild?.type === "try") { + result.add({ + label: "catch", + kind: CompletionItemKind.Snippet, + insertTextFormat: InsertTextFormat.Snippet, + insertText: "catch (${1:e}) {\n\t$0\n}", + weight: CompletionWeight.CONTEXT_ELEMENT, + }) + } } }