Skip to content

Commit

Permalink
left test tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
ecmel committed May 21, 2024
1 parent 906a5a5 commit b12861f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 56 deletions.
21 changes: 17 additions & 4 deletions server/src/parser/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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 &&
Expand Down Expand Up @@ -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);
Expand Down
82 changes: 30 additions & 52 deletions server/src/qLangServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ import {
assigned,
lambda,
assignable,
qualified,
inParam,
namespace,
relative,
} from "./parser";
import { lint } from "./linter";

Expand Down Expand Up @@ -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 = <Token>{
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({
Expand All @@ -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[] {
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit b12861f

Please sign in to comment.