From b12861f39c7d3fba69bf37756d8338e902de9196 Mon Sep 17 00:00:00 2001 From: ecmel Date: Tue, 21 May 2024 16:49:28 +0300 Subject: [PATCH] left test tokens --- server/src/parser/utils.ts | 21 ++++++++-- server/src/qLangServer.ts | 82 ++++++++++++++------------------------ 2 files changed, 47 insertions(+), 56 deletions(-) diff --git a/server/src/parser/utils.ts b/server/src/parser/utils.ts index 668e5d73..a1ff6444 100644 --- a/server/src/parser/utils.ts +++ b/server/src/parser/utils.ts @@ -77,8 +77,17 @@ export function inParam(token: Token) { return lambda && bracket && lambda.tangled === bracket; } +export function namespace(token: Token) { + const parts = identifier(token).split(".", 3); + return (parts.length === 3 && parts[1]) || ""; +} + export function qualified(token: Token) { - return token.image.startsWith("."); + if (token.image.startsWith(".")) { + const parts = token.image.split(".", 3); + return parts.length === 3 && !!parts[1]; + } + return false; } export function identifier(token: Token) { @@ -89,6 +98,12 @@ export function identifier(token: Token) { : token.image; } +export function relative(token: Token, source: Token | undefined) { + return source?.namespace + ? identifier(token).replace(`.${source.namespace}.`, "") + : token.image; +} + export function lambda(token?: Token) { return ( token && @@ -201,9 +216,7 @@ export function findIdentifiers( ); result.forEach((token) => { const found = completions.find( - (target) => - target.image === token.image && - target.namespace === token.namespace, + (target) => identifier(token) === identifier(target), ); if (!found) { completions.push(token); diff --git a/server/src/qLangServer.ts b/server/src/qLangServer.ts index 43bbe4cc..1656948d 100644 --- a/server/src/qLangServer.ts +++ b/server/src/qLangServer.ts @@ -50,8 +50,9 @@ import { assigned, lambda, assignable, - qualified, inParam, + namespace, + relative, } from "./parser"; import { lint } from "./linter"; @@ -173,16 +174,22 @@ export default class QLangServer { }: RenameParams): WorkspaceEdit | null { const tokens = this.parse(textDocument); const source = positionToToken(tokens, position); - const edits = findIdentifiers(FindKind.Rename, tokens, source).map( - (token) => createTextEdit(token, newName), - ); - return edits.length === 0 - ? null - : { - changes: { - [textDocument.uri]: edits, - }, - }; + const refs = findIdentifiers(FindKind.Rename, tokens, source); + if (refs.length === 0) { + return null; + } + const name = { + image: newName, + namespace: source?.namespace, + }; + const edits = refs.map((token) => { + return TextEdit.replace(rangeFromToken(token), relative(name, token)); + }); + return { + changes: { + [textDocument.uri]: edits, + }, + }; } public onCompletion({ @@ -191,9 +198,18 @@ export default class QLangServer { }: CompletionParams): CompletionItem[] { const tokens = this.parse(textDocument); const source = positionToToken(tokens, position); - return findIdentifiers(FindKind.Completion, tokens, source).map((token) => - createCompletionItem(token, source), - ); + return findIdentifiers(FindKind.Completion, tokens, source).map((token) => { + return { + label: token.image, + labelDetails: { + detail: ` .${namespace(token)}`, + }, + insertText: relative(token, source), + kind: lambda(assigned(token)) + ? CompletionItemKind.Function + : CompletionItemKind.Variable, + }; + }); } private parse(textDocument: TextDocumentIdentifier): Token[] { @@ -226,44 +242,6 @@ function positionToToken(tokens: Token[], position: Position) { }); } -function relative(token: Token, namespace: string | undefined) { - if (qualified(token)) { - const [_dot, target, image] = token.image.split(/\./g, 3); - if (image && target === namespace) { - return image; - } - } - if (token.namespace === namespace) { - return token.image; - } - return identifier(token); -} - -function createCompletionItem(token: Token, source: Token | undefined) { - return { - label: token.image, - labelDetails: { - detail: ` ${identifier(token)}`, - }, - insertText: relative(token, source?.namespace), - kind: lambda(assigned(token)) - ? CompletionItemKind.Function - : CompletionItemKind.Variable, - }; -} - -function createTextEdit(token: Token, newName: string) { - if (!newName.startsWith(".")) { - if (qualified(token)) { - const [_dot, namespace, image] = token.image.split(/\./g, 3); - if (image) { - newName = `.${namespace}.${newName}`; - } - } - } - return TextEdit.replace(rangeFromToken(token), newName); -} - function createSymbol(token: Token, tokens: Token[]): DocumentSymbol { const range = rangeFromToken(token); return DocumentSymbol.create(