Skip to content

Commit

Permalink
feat: add document links provider
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 13, 2022
1 parent 633a0cd commit 771a6e7
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/olive-mails-clap.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"marko-vscode": patch
"@marko/language-server": patch
---

Implements document links provider (currently for stylesheets).
11 changes: 11 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ connection.onInitialize(() => {
renameProvider: true,
codeActionProvider: true,
referencesProvider: true,
documentLinkProvider: { resolveProvider: false },
colorProvider: true,
documentHighlightProvider: true,
completionProvider: {
Expand Down Expand Up @@ -114,6 +115,16 @@ connection.onReferences(async (params, cancel) => {
);
});

connection.onDocumentLinks(async (params, cancel) => {
return (
(await service.findDocumentLinks(
documents.get(params.textDocument.uri)!,
params,
cancel
)) || null
);
});

connection.onDocumentHighlight(async (params, cancel) => {
return (
(await service.findDocumentHighlights(
Expand Down
25 changes: 25 additions & 0 deletions server/src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
DefinitionLink,
Diagnostic,
DocumentHighlight,
DocumentLink,
Location,
WorkspaceEdit,
} from "vscode-languageserver";
Expand Down Expand Up @@ -99,6 +100,30 @@ const service: Plugin = {

return result;
},
async findDocumentLinks(doc, params, cancel) {
let result: DocumentLink[] | undefined;

try {
const requests = plugins.map((plugin) =>
plugin.findDocumentLinks?.(doc, params, cancel)
);
for (const pending of requests) {
const cur = await pending;
if (cancel.isCancellationRequested) return;
if (cur) {
if (result) {
result.push(...cur);
} else {
result = cur;
}
}
}
} catch (err) {
displayError(err);
}

return result;
},
async findDocumentHighlights(doc, params, cancel) {
let result: DocumentHighlight[] | undefined;

Expand Down
31 changes: 31 additions & 0 deletions server/src/service/stylesheet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TextDocumentEdit,
Location,
TextEdit,
DocumentLink,
} from "vscode-languageserver";
import {
getCSSLanguageService,
Expand All @@ -20,6 +21,7 @@ import { getCompilerInfo, parse } from "../../utils/compiler";
import { START_OF_FILE } from "../../utils/utils";
import type { Plugin } from "../types";
import { extractStyleSheets } from "./extract";
import { displayInformation } from "../../utils/messages";

interface StyleSheetInfo {
virtualDoc: TextDocument;
Expand Down Expand Up @@ -132,6 +134,35 @@ const StyleSheetService: Partial<Plugin> = {
return result.length ? result : undefined;
}
},
async findDocumentLinks(doc) {
const infoByExt = getStyleSheetInfo(doc);
const result: DocumentLink[] = [];

for (const ext in infoByExt) {
const info = infoByExt[ext];
const { service, virtualDoc } = info;

for (const link of service.findDocumentLinks(virtualDoc, info.parsed, {
resolveReference(ref, baseUrl) {
const resolved = new URL(ref, new URL(baseUrl, "resolve://"));
if (resolved.protocol === "resolve:") {
// `baseUrl` is a relative URL.
return resolved.pathname + resolved.search + resolved.hash;
}
return resolved.toString();
},
})) {
if (link.target && updateRange(doc, info, link.range)) {
result.push(link);
}
}
}

if (result.length) {
displayInformation(result.map((it) => it.target).join(","));
return result;
}
},
async findDocumentHighlights(doc, params) {
const infoByExt = getStyleSheetInfo(doc);
const sourceOffset = doc.offsetAt(params.position);
Expand Down
3 changes: 3 additions & 0 deletions server/src/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import type {
DocumentFormattingParams,
DocumentHighlight,
DocumentHighlightParams,
DocumentLink,
DocumentLinkParams,
Hover,
HoverParams,
Location,
Expand Down Expand Up @@ -44,6 +46,7 @@ export type Plugin = {
Location | LocationLink | (Location | LocationLink)[]
>;
findReferences: Handler<ReferenceParams, Location[]>;
findDocumentLinks: Handler<DocumentLinkParams, DocumentLink[]>;
findDocumentHighlights: Handler<DocumentHighlightParams, DocumentHighlight[]>;
findDocumentColors: Handler<DocumentColorParams, ColorInformation[]>;
getColorPresentations: Handler<ColorPresentationParams, ColorPresentation[]>;
Expand Down

0 comments on commit 771a6e7

Please sign in to comment.