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
36 changes: 34 additions & 2 deletions server/src/e2e/tolk/testcases/rename/basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,6 @@ Rename builtin type
========================================================================
fun test(): int {
//! ^ bool
}
}
------------------------------------------------------------------------
The element can't be renamed.
Expand All @@ -491,7 +490,40 @@ Rename stdlib function
fun test(): int {
minMax();
//! ^ otherFunc
}
}
------------------------------------------------------------------------
The element can't be renamed.

========================================================================
Wrap in backtick for keyword name
========================================================================
fun foo() {}

fun test(): int {
foo();
//! ^ return
}
------------------------------------------------------------------------
fun `return`() {}

fun test(): int {
`return`();
//! ^ return
}

========================================================================
Wrap in backtick for keyword name 2
========================================================================
fun foo() {}

fun test(): int {
foo();
//! ^ match
}
------------------------------------------------------------------------
fun `match`() {}

fun test(): int {
`match`();
//! ^ match
}
35 changes: 35 additions & 0 deletions server/src/languages/tolk/lang/names-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,38 @@ export function trimBackticks(text: string): string {
}
return text
}

export const KEYWORDS = new Set([
"tolk",
"import",
"global",
"const",
"type",
"struct",
"fun",
"get",
"mutate",
"asm",
"builtin",
"var",
"val",
"return",
"repeat",
"if",
"else",
"do",
"while",
"break",
"continue",
"throw",
"assert",
"try",
"catch",
"lazy",
"is",
"!is",
"match",
"true",
"false",
"null",
])
2 changes: 2 additions & 0 deletions server/src/languages/tolk/rename/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {NamedNode} from "@server/languages/tolk/psi/TolkNode"
import {Reference} from "@server/languages/tolk/psi/Reference"
import type {Node as SyntaxNode} from "web-tree-sitter"
import type {Position} from "vscode-languageclient"
import {KEYWORDS} from "@server/languages/tolk/lang/names-util"

export function provideTolkRename(params: lsp.RenameParams, file: TolkFile): WorkspaceEdit | null {
const renameNode = findRenameTarget(params, file)
Expand Down Expand Up @@ -82,6 +83,7 @@ export function provideTolkRenamePrepare(
}

function isValidIdentifier(name: string): boolean {
if (KEYWORDS.has(name)) return false
return /^[A-Z_a-z]\w*$/.test(name)
}

Expand Down