Skip to content

Commit

Permalink
feat: implement hover provider for tag names
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 18, 2022
1 parent 7ebc777 commit 7bb0f9a
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
6 changes: 6 additions & 0 deletions .changeset/tender-peaches-yell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"marko-vscode": patch
"@marko/language-server": patch
---

Implement hover provider for tag names.
30 changes: 30 additions & 0 deletions server/src/service/marko/hover/OpenTagName.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { Node } from "../../../utils/parser";
import { getDocFile } from "../../../utils/doc-file";
import type { HoverMeta, HoverResult } from ".";
import getTagNameCompletion from "../util/get-tag-name-completion";
import { START_OF_FILE } from "../../../utils/utils";
import type { MarkupContent } from "vscode-languageserver";

export function OpenTagName({
document,
lookup,
parsed,
node,
}: HoverMeta<Node.OpenTagName>): HoverResult {
const importer = getDocFile(document);
const tag = node.parent;
const range = parsed.locationAt(node);
const tagDef = tag.nameText && lookup.getTag(tag.nameText);

if (tagDef) {
const completion = getTagNameCompletion({
tag: tagDef,
range: START_OF_FILE,
importer,
});
return {
range,
contents: completion.documentation as MarkupContent,
};
}
}
36 changes: 36 additions & 0 deletions server/src/service/marko/hover/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { TextDocument } from "vscode-languageserver-textdocument";
import type { HoverParams, Hover } from "vscode-languageserver";
import { getCompilerInfo, parse } from "../../../utils/compiler";
import type { Plugin, Result } from "../../types";
import { NodeType } from "../../../utils/parser";
import { OpenTagName } from "./OpenTagName";

export type HoverResult = Result<Hover>;
export interface HoverMeta<N = unknown>
extends ReturnType<typeof getCompilerInfo> {
document: TextDocument;
params: HoverParams;
parsed: ReturnType<typeof parse>;
offset: number;
code: string;
node: N;
}

const handlers: Record<string, (data: HoverMeta<any>) => HoverResult> = {
OpenTagName,
};

export const doHover: Plugin["doHover"] = async (doc, params) => {
const parsed = parse(doc);
const offset = doc.offsetAt(params.position);
const node = parsed.nodeAt(offset);
return await handlers[NodeType[node.type]]?.({
document: doc,
params,
parsed,
offset,
node,
code: doc.getText(),
...getCompilerInfo(doc),
});
};
2 changes: 2 additions & 0 deletions server/src/service/marko/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Plugin } from "../types";
import { doComplete } from "./complete";
import { doValidate } from "./validate";
import { doHover } from "./hover";
import { findDefinition } from "./definition";
import { findDocumentLinks } from "./document-links";
import { findDocumentSymbols } from "./document-symbols";
Expand All @@ -9,6 +10,7 @@ import { format } from "./format";
export default {
doComplete,
doValidate,
doHover,
findDefinition,
findDocumentLinks,
findDocumentSymbols,
Expand Down
12 changes: 6 additions & 6 deletions server/src/service/marko/util/get-tag-name-completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ export default function getTagNameCompletion({
);

const nodeModuleName = nodeModuleMatch && nodeModuleMatch[1];
const isCoreTag = nodeModuleName === "marko";

const isCoreTag =
/^@?marko[/-]/.test(tag.taglibId) || nodeModuleName === "marko";
const documentation = {
kind: MarkupKind.Markdown,
value: tag.html
? `Built in [<${tag.name}>](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/${tag.name}) HTML tag.`
? `Built in [&lt;${tag.name}&gt;](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/${tag.name}) HTML tag.`
: isCoreTag
? `Core Marko &lt;${tag.name}&gt; tag.`
: nodeModuleName
? isCoreTag
? `Core Marko [<${tag.name}>](${fileURIForTag}) tag.`
: `Custom Marko tag discovered from the ["${nodeModuleName}"](${fileURIForTag}) npm package.`
? `Custom Marko tag discovered from the ["${nodeModuleName}"](${fileURIForTag}) npm package.`
: `Custom Marko tag discovered from:\n\n[${
importer ? path.relative(importer, fileForTag) : fileForTag
}](${fileURIForTag})`,
Expand Down

0 comments on commit 7bb0f9a

Please sign in to comment.