Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions server/src/e2e/tolk/testcases/completion/local.test
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ fun foo() {
}
}
------------------------------------------------------------------------
5 error any
5 error int

========================================================================
Variables completion from catch clause
Expand All @@ -45,8 +45,8 @@ fun foo() {
}
}
------------------------------------------------------------------------
5 error any
5 errorData any
5 error int
5 errorData unknown

========================================================================
Variables completion from match expression
Expand Down
30 changes: 30 additions & 0 deletions server/src/e2e/tolk/testcases/completion/snippets.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
========================================================================
Catch snippet
========================================================================
fun test() {
try {
// ...
} cat<caret>
}
------------------------------------------------------------------------
14 catch

========================================================================
Catch snippet as expression
========================================================================
fun test() {
cat<caret>
}
------------------------------------------------------------------------
No completion items

========================================================================
No completion for catch variable name
========================================================================
const eeee = 100

fun test() {
try {} catch (e<caret>) {}
}
------------------------------------------------------------------------
No completion items
6 changes: 6 additions & 0 deletions server/src/languages/tolk/completion/CompletionContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
// <caret>
Expand Down Expand Up @@ -127,6 +132,7 @@ export class CompletionContext {
!this.insideImport &&
!this.structTopLevel &&
!this.expectMatchArm &&
!this.catchVariable &&
!this.isAnnotationName
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export class ReferenceCompletionProvider implements CompletionProvider<Completio
!ctx.insideImport &&
!ctx.isAnnotationName &&
!ctx.structTopLevel &&
!ctx.catchVariable &&
!ctx.expectMatchArm
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class SnippetsCompletionProvider implements CompletionProvider<Completion
return ctx.isStatement && !ctx.topLevel && !ctx.afterDot
}

public addCompletion(_ctx: CompletionContext, result: CompletionResult): void {
public addCompletion(ctx: CompletionContext, result: CompletionResult): void {
result.add({
label: "val",
kind: CompletionItemKind.Snippet,
Expand Down Expand Up @@ -99,5 +99,18 @@ export class SnippetsCompletionProvider implements CompletionProvider<Completion
insertText: "try {\n\t${1}\n} catch (e) {\n\t${2}\n}",
weight: CompletionWeight.SNIPPET,
})

// if:
// try { ... } <caret>
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,
})
}
}
}
Loading