pss-langserver
Version:
A Language server for the Portable Stimulus Standard
106 lines (105 loc) • 4.05 kB
JavaScript
;
/*
* Copyright (C) 2025 Darshan(@thisisthedarshan)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getHoverFor = getHoverFor;
exports.buildHoverItems = buildHoverItems;
exports.createBuiltinHoverCache = createBuiltinHoverCache;
const node_1 = require("vscode-languageserver/node");
const gotoProvider_1 = require("./gotoProvider");
const keywords_1 = require("../definitions/keywords");
const builtinFunctions_1 = require("../definitions/builtinFunctions");
const helpers_1 = require("../parser/helpers");
const objectCommentsProvider_1 = require("./objectCommentsProvider");
function getHoverFor(hoverRecords, content, pos) {
const key = (0, gotoProvider_1.wordAt)(content, pos);
if (key == null) {
return null;
}
const hover = hoverRecords.find(record => key in record)?.[key] ?? null;
return hover;
}
function getHoverData(item) {
let hoverInfo = [];
const comments = (typeof item.comments === 'string') ? item.comments : (0, helpers_1.buildMarkdownComment)(item.comments);
if (comments.length != 0) {
const hoverData = {
contents: {
kind: node_1.MarkupKind.Markdown,
value: comments
}
};
hoverInfo.push({ [item.name]: hoverData });
}
else {
/* This new logic just shows hover info for basic items */
const comment = (0, objectCommentsProvider_1.createCommentsFromNodeMarkdown)(item);
const hoverData = {
contents: {
kind: node_1.MarkupKind.Markdown,
value: comment
}
};
hoverInfo.push({ [item.name]: hoverData });
}
item.children.forEach(child => {
hoverInfo = [...hoverInfo, ...getHoverData(child)];
});
return hoverInfo;
}
function buildHoverItems(ast) {
let hoverRecords = [];
ast.forEach(object => {
const existingItem = hoverRecords.some(record => object.name in record);
if (!existingItem) {
hoverRecords = [...hoverRecords, ...getHoverData(object)];
}
});
return hoverRecords;
}
function createBuiltinHoverCache() {
let hoverRecords = [];
keywords_1.keywords.list.forEach((key, index) => {
const hoverData = {
contents: {
kind: node_1.MarkupKind.Markdown,
value: `*${key}*\n${keywords_1.keywords.descriptions[index]}`
}
};
hoverRecords.push({ [key]: hoverData });
});
for (const [keyword, funcInfo] of Object.entries(builtinFunctions_1.builtInSignatures)) {
let documentation = "```pss\n" + funcInfo.signature + "\n```\n";
documentation += `*${keyword}* is a part of the **${funcInfo.package}**\n`;
documentation += `#### Parameters\n`;
funcInfo.parameters.forEach(param => {
documentation += `- **${param.label}**: ${param.documentation}\n`;
});
const hoverData = {
contents: {
kind: node_1.MarkupKind.Markdown,
value: documentation
}
};
const index = hoverRecords.findIndex(record => keyword in record);
if (index >= 0)
hoverRecords[index][keyword] = hoverData; /* Overwrite */
else
hoverRecords.push({ [keyword]: hoverData }); /* Add new */
}
return hoverRecords;
}