greybel-languageserver-core
Version:
Core functionality of language server for GreyScript
105 lines • 4.88 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.activate = void 0;
const greybel_core_1 = require("greybel-core");
const greybel_type_analyzer_1 = require("greybel-type-analyzer");
const greyscript_core_1 = require("greyscript-core");
const meta_utils_1 = require("meta-utils");
const path_1 = __importDefault(require("path"));
const document_uri_builder_1 = require("../helper/document-manager/document-uri-builder");
const lookup_type_1 = require("../helper/lookup-type");
const markdown_string_1 = require("../helper/markdown-string");
const tooltip_1 = require("../helper/tooltip");
const types_1 = require("../types");
function activate(context) {
async function generateImportCodeHover(textDocument, astResult) {
const hoverText = new markdown_string_1.MarkdownString('');
const importAst = astResult.closest;
const documentUriBuilder = await document_uri_builder_1.DocumentURIBuilder.fromTextDocument(textDocument, context);
const target = await documentUriBuilder.getPathWithContext(importAst.directory, context);
const output = target == null
? ['Cannot open file.']
: [
`[Imports file "${path_1.default.basename(target)}" inside this code](${target})`,
'***',
'Click the link above to open the file.',
'',
'Use the build command to create an installer',
'file which will bundle all dependencies.'
];
hoverText.appendMarkdown(output.join('\n'));
return {
contents: hoverText.toString()
};
}
async function generateImportHover(textDocument, astResult) {
// shows link to import/include resource
const hoverText = new markdown_string_1.MarkdownString('');
const importCodeAst = astResult.closest;
const fileDir = importCodeAst.path;
const documentUriBuilder = await document_uri_builder_1.DocumentURIBuilder.fromTextDocument(textDocument, context);
const target = await documentUriBuilder.getPathWithContext(fileDir, context);
const output = target == null
? ['Cannot open file.']
: [
`[Inserts file "${path_1.default.basename(target)}" inside this code when building](${target})`,
'***',
'Click the link above to open the file.'
];
hoverText.appendMarkdown(output.join('\n'));
return {
contents: hoverText.toString()
};
}
context.connection.onHover(async (params) => {
if (!context.getConfiguration().hoverdocs) {
return;
}
const document = await context.fs.getTextDocument(params.textDocument.uri);
if (document == null) {
return;
}
const activeDocument = context.documentManager.get(document);
const helper = new lookup_type_1.LookupHelper(activeDocument, context);
const astResult = await helper.lookupAST(params.position);
if (!astResult) {
return;
}
if (astResult.closest.type === greyscript_core_1.ASTType.ImportCodeExpression) {
return await generateImportCodeHover(document, astResult);
}
else if (astResult.closest.type === greybel_core_1.ASTType.FeatureImportExpression ||
astResult.closest.type === greybel_core_1.ASTType.FeatureIncludeExpression) {
return await generateImportHover(document, astResult);
}
const entity = await helper.lookupTypeInfo(astResult);
if (!entity) {
return;
}
if ((0, greybel_type_analyzer_1.isFunctionType)(entity.item) ||
((0, greybel_type_analyzer_1.isUnionType)(entity.item) && entity.item.variants.some(greybel_type_analyzer_1.isFunctionType))) {
return (0, tooltip_1.createHover)(entity);
}
const hoverText = new markdown_string_1.MarkdownString('');
const metaTypes = entity.item
.toMeta()
.map(meta_utils_1.SignatureDefinitionTypeMeta.parse);
const displayName = entity.value
? entity.value
: entity.path;
let label = `(${(0, tooltip_1.formatKind)(entity.completionItemKind)}) ${displayName}: ${(0, tooltip_1.formatTypes)(metaTypes)}`;
const labelBody = (0, tooltip_1.createTypeBody)(entity.item);
if (labelBody) {
label += ` ${JSON.stringify(labelBody, null, 2)}`;
}
hoverText.appendCodeblock(types_1.LanguageId, label);
return {
contents: hoverText.toString()
};
});
}
exports.activate = activate;
//# sourceMappingURL=hover.js.map