-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement document symbols provider
- Loading branch information
1 parent
6906b39
commit 7ebc777
Showing
10 changed files
with
164 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
--- | ||
"marko-vscode": patch | ||
"@marko/language-server": patch | ||
--- | ||
|
||
Implement document symbols provider. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { URI } from "vscode-uri"; | ||
import type { TaglibLookup } from "@marko/babel-utils"; | ||
import { SymbolInformation, SymbolKind } from "vscode-languageserver"; | ||
import type { TextDocument } from "vscode-languageserver-textdocument"; | ||
import { type Node, type parse, NodeType } from "../../../utils/parser"; | ||
|
||
/** | ||
* Iterate over the Marko CST and extract all the symbols (mostly tags) in the document. | ||
*/ | ||
export function extractDocumentSymbols( | ||
doc: TextDocument, | ||
parsed: ReturnType<typeof parse>, | ||
lookup: TaglibLookup | ||
): SymbolInformation[] { | ||
if (URI.parse(doc.uri).scheme === "untitled") { | ||
return []; | ||
} | ||
|
||
const symbols: SymbolInformation[] = []; | ||
const { program } = parsed; | ||
const visit = (node: Node.ChildNode) => { | ||
switch (node.type) { | ||
case NodeType.Tag: | ||
case NodeType.AttrTag: | ||
symbols.push( | ||
SymbolInformation.create( | ||
(node.type === NodeType.AttrTag | ||
? node.nameText?.slice(node.nameText.indexOf("@")) | ||
: node.nameText) || "<${...}>", | ||
(node.nameText && | ||
lookup.getTag(node.nameText)?.html && | ||
SymbolKind.Property) || | ||
SymbolKind.Class, | ||
parsed.locationAt(node), | ||
doc.uri | ||
) | ||
); | ||
|
||
if (node.body) { | ||
for (const child of node.body) { | ||
visit(child); | ||
} | ||
} | ||
|
||
break; | ||
} | ||
}; | ||
|
||
for (const item of program.body) { | ||
visit(item); | ||
} | ||
|
||
return symbols; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { SymbolInformation } from "vscode-languageserver"; | ||
import { getCompilerInfo, parse } from "../../../utils/compiler"; | ||
import type { Plugin } from "../../types"; | ||
import { extractDocumentSymbols } from "./extract"; | ||
|
||
const cache = new WeakMap<ReturnType<typeof parse>, SymbolInformation[]>(); | ||
|
||
export const findDocumentSymbols: Plugin["findDocumentSymbols"] = async ( | ||
doc | ||
) => { | ||
const parsed = parse(doc); | ||
let result = cache.get(parsed); | ||
if (!result) { | ||
result = extractDocumentSymbols(doc, parsed, getCompilerInfo(doc).lookup); | ||
cache.set(parsed, result); | ||
} | ||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters