Skip to content

Commit

Permalink
chore: implement references provider
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Jul 11, 2022
1 parent 0070dc0 commit 2f9195b
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/soft-garlics-raise.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"marko-vscode": patch
"@marko/language-server": patch
---

Implements references provider (currently for stylesheets).
49 changes: 49 additions & 0 deletions clients/vscode/src/__tests__/references.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import vscode from "vscode";
import snap from "mocha-snap";
import { getTestDoc, getTestEditor, updateTestDoc } from "./setup.test";

describe("references", () => {
it("css custom property", async () => {
await snap.inline(
() =>
references(
`style {
:root {
--color█: green;
}
body {
color: var(--color);
}
}`
),
`[
{
offset: 24,
value: '--color'
},
{
offset: 71,
value: '--color'
},
[length]: 2
]`
);
});
});

async function references(src: string) {
const doc = getTestDoc();
const editor = getTestEditor();
await updateTestDoc(src);
const locations = await vscode.commands.executeCommand<vscode.Location[]>(
"vscode.executeReferenceProvider",
doc.uri,
editor.selection.start
);

return locations.map((it) => ({
offset: doc.offsetAt(it.range.start),
value: doc.getText(it.range),
}));
}
11 changes: 11 additions & 0 deletions server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ connection.onInitialize(() => {
hoverProvider: true,
renameProvider: true,
codeActionProvider: true,
referencesProvider: true,
colorProvider: true,
documentHighlightProvider: true,
completionProvider: {
Expand Down Expand Up @@ -103,6 +104,16 @@ connection.onDefinition(async (params, cancel) => {
);
});

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

connection.onDocumentHighlight(async (params, cancel) => {
return (
(await service.findDocumentHighlights(
Expand Down
24 changes: 24 additions & 0 deletions server/src/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,30 @@ const service: Plugin = {

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

try {
const requests = plugins.map((plugin) =>
plugin.findReferences?.(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
27 changes: 27 additions & 0 deletions server/src/service/stylesheet/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
InsertReplaceEdit,
Range,
TextDocumentEdit,
Location,
TextEdit,
} from "vscode-languageserver";
import {
Expand Down Expand Up @@ -105,6 +106,32 @@ const StyleSheetService: Partial<Plugin> = {
break;
}
},
async findReferences(doc, params) {
const infoByExt = getStyleSheetInfo(doc);
const sourceOffset = doc.offsetAt(params.position);

for (const ext in infoByExt) {
const info = infoByExt[ext];
// Find the first stylesheet data that contains the offset.
const generatedOffset = info.generatedOffsetAt(sourceOffset);
if (generatedOffset === undefined) continue;

const { service, virtualDoc } = info;
const result: Location[] = [];

for (const location of service.findReferences(
virtualDoc,
virtualDoc.positionAt(generatedOffset),
info.parsed
)) {
if (updateRange(doc, info, location.range)) {
result.push(location);
}
}

return result.length ? result : undefined;
}
},
async findDocumentHighlights(doc, params) {
const infoByExt = getStyleSheetInfo(doc);
const sourceOffset = doc.offsetAt(params.position);
Expand Down
2 changes: 2 additions & 0 deletions server/src/service/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
HoverParams,
Location,
LocationLink,
ReferenceParams,
RenameParams,
TextEdit,
WorkspaceEdit,
Expand All @@ -42,6 +43,7 @@ export type Plugin = {
DefinitionParams,
Location | LocationLink | (Location | LocationLink)[]
>;
findReferences: Handler<ReferenceParams, Location[]>;
findDocumentHighlights: Handler<DocumentHighlightParams, DocumentHighlight[]>;
findDocumentColors: Handler<DocumentColorParams, ColorInformation[]>;
getColorPresentations: Handler<ColorPresentationParams, ColorPresentation[]>;
Expand Down

0 comments on commit 2f9195b

Please sign in to comment.