Skip to content

Commit

Permalink
add hover provider
Browse files Browse the repository at this point in the history
Co-authored-by: mabagoury <[email protected]>
  • Loading branch information
2 people authored and jdesrosiers committed Apr 24, 2024
1 parent 4740515 commit dc14ea4
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 177 deletions.
44 changes: 42 additions & 2 deletions language-server/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import {
TextDocuments,
TextDocumentSyncKind,
CompletionItemKind,
FileChangeType
FileChangeType,
MarkupKind
} from "vscode-languageserver/node.js";
import { TextDocument } from "vscode-languageserver-textdocument";
import { readFile } from "node:fs/promises";
Expand Down Expand Up @@ -73,7 +74,8 @@ connection.onInitialize(({ capabilities, workspaceFolders }) => {
completionProvider: {
resolveProvider: false,
triggerCharacters: ["\"", ":", " "]
}
},
hoverProvider: true
};

if (hasWorkspaceFolderCapability) {
Expand Down Expand Up @@ -403,5 +405,43 @@ const trailingHashDialects = new Set([
]);
const shouldHaveTrailingHash = (uri) => trailingHashDialects.has(uri);

// KEYWORD HOVER

connection.onHover(async (textDocumentPositionParams) => {
const { textDocument: { uri: textDocumentURI }, position } = textDocumentPositionParams;
const document = documents.get(textDocumentURI);

const schemaResources = await getSchemaResources(document);
for (const { dialectUri, schemaInstance } of schemaResources) {
if (!hasDialect(dialectUri)) {
continue;
}

try {
const annotations = await annotate(dialectUri, schemaInstance);
const keyword = annotations.getInstanceAtPosition(position);
if (keyword.typeOf() !== "undefined") {
// Found
const description = keyword.annotation("description", dialectUri).join("\n");
return buildHover(MarkupKind.Markdown, description, keyword.startPosition(), keyword.endPosition());
}
} catch (error) {
if (error instanceof ValidationError) {
return null;
}
}
}
});

const buildHover = (kind, value, startPosition, endPosition) => {
return {
contents: { kind, value },
range: {
start: startPosition,
end: endPosition
}
};
};

connection.listen();
documents.listen(connection);
Loading

0 comments on commit dc14ea4

Please sign in to comment.