-
Notifications
You must be signed in to change notification settings - Fork 56
Add code symbols into outline #972
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
vezwork
wants to merge
7
commits into
main
Choose a base branch
from
feat/code-cell-symbols
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+390
−1
Open
Changes from 4 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f6fafb2
Add code symbols into outline
vezwork d18863a
Add changelog entry
vezwork 93b156b
Merge branch 'main' into feat/code-cell-symbols
vezwork 14c0513
Merge branch 'main' into feat/code-cell-symbols
vezwork ed4207b
Add handling for SymbolInformation in getCodeCellSymbols
vezwork a09c41b
Add code cell symbols rety logic
vezwork b02a825
Add tests
vezwork File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -26,7 +26,10 @@ import { | |
| Uri, | ||
| Diagnostic, | ||
| window, | ||
| ColorThemeKind | ||
| ColorThemeKind, | ||
| DocumentSymbol, | ||
| Range, | ||
| SymbolKind, | ||
| } from "vscode"; | ||
| import { | ||
| LanguageClient, | ||
|
|
@@ -48,6 +51,7 @@ import { | |
| ProvideDefinitionSignature, | ||
| ProvideHoverSignature, | ||
| ProvideSignatureHelpSignature, | ||
| ProvideDocumentSymbolsSignature, | ||
| State, | ||
| HandleDiagnosticsSignature | ||
| } from "vscode-languageclient"; | ||
|
|
@@ -57,6 +61,7 @@ import { | |
| unadjustedRange, | ||
| virtualDoc, | ||
| withVirtualDocUri, | ||
| VirtualDocStyle, | ||
| } from "../vdoc/vdoc"; | ||
| import { isVirtualDoc } from "../vdoc/vdoc-tempfile"; | ||
| import { activateVirtualDocEmbeddedContent } from "../vdoc/vdoc-content"; | ||
|
|
@@ -72,6 +77,8 @@ import { imageHover } from "../providers/hover-image"; | |
| import { LspInitializationOptions, QuartoContext } from "quarto-core"; | ||
| import { extensionHost } from "../host"; | ||
| import semver from "semver"; | ||
| import { EmbeddedLanguage } from "../vdoc/languages"; | ||
| import { SymbolInformation } from "vscode"; | ||
|
|
||
| let client: LanguageClient; | ||
|
|
||
|
|
@@ -113,6 +120,7 @@ export async function activateLsp( | |
| engine | ||
| ), | ||
| provideDocumentSemanticTokens: embeddedSemanticTokensProvider(engine), | ||
| provideDocumentSymbols: embeddedDocumentSymbolProvider(engine), | ||
| }; | ||
| if (config.get("cells.hoverHelp.enabled", true)) { | ||
| middleware.provideHover = embeddedHoverProvider(engine); | ||
|
|
@@ -364,6 +372,120 @@ function isWithinYamlComment(doc: TextDocument, pos: Position) { | |
| return !!line.match(/^\s*#\s*\| /); | ||
| } | ||
|
|
||
| const isDocumentSymbol = (a: Object): a is DocumentSymbol => { | ||
| return ('range' in a && 'selectionRange' in a); | ||
| }; | ||
|
|
||
| /** | ||
| * Enhances document symbols by adding code symbols from embedded languages to code cells | ||
| */ | ||
| function embeddedDocumentSymbolProvider(engine: MarkdownEngine) { | ||
| return async ( | ||
| document: TextDocument, | ||
| token: CancellationToken, | ||
| next: ProvideDocumentSymbolsSignature | ||
| ): Promise<DocumentSymbol[] | SymbolInformation[] | undefined> => { | ||
| // Get base symbols from LSP (headers, code cells, etc.) | ||
| const baseSymbols = await next(document, token); | ||
|
|
||
| if (!baseSymbols || token.isCancellationRequested) { | ||
| return baseSymbols ?? undefined; | ||
| } | ||
|
|
||
| // Check if we got DocumentSymbol[] (can be enhanced) or SymbolInformation[] (cannot) | ||
| // I don't think we actually ever get SymbolInformation[] here, but I'm not certain | ||
| // so this is defensively coded. | ||
| if (baseSymbols.length > 0 && isDocumentSymbol(baseSymbols[0])) { | ||
| return await enhanceSymbolsWithCodeCellContent(document, baseSymbols as DocumentSymbol[], engine, token); | ||
| } | ||
|
|
||
| return baseSymbols; | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Finds code cell symbols, makes vdocs for them, gets symbols from the vdoc, and nests those symbols | ||
| * under the code cell's symbol. | ||
| */ | ||
| async function enhanceSymbolsWithCodeCellContent( | ||
| document: TextDocument, | ||
| symbols: DocumentSymbol[], | ||
| engine: MarkdownEngine, | ||
| token: CancellationToken | ||
| ): Promise<DocumentSymbol[]> { | ||
| const enhanced: DocumentSymbol[] = []; | ||
|
|
||
| for (const symbol of symbols) { | ||
| if (token.isCancellationRequested) return symbols; | ||
|
|
||
| // Check if this is a code cell symbol (SymbolKind.Function indicates code cells from toc.ts) | ||
| if (symbol.kind === SymbolKind.Function) { | ||
| symbol.children = [ | ||
| ...symbol.children, | ||
| ...(await getCodeCellSymbols(document, symbol.range, engine) || []) | ||
| ]; | ||
| } else { | ||
| symbol.children = | ||
| await enhanceSymbolsWithCodeCellContent(document, symbol.children, engine, token); | ||
| } | ||
|
|
||
| enhanced.push(symbol); | ||
| } | ||
|
|
||
| return enhanced; | ||
| } | ||
|
|
||
| /** | ||
| * Gets symbols from an embedded language for a code cell | ||
| */ | ||
| async function getCodeCellSymbols( | ||
| document: TextDocument, | ||
| cellRange: Range, | ||
| engine: MarkdownEngine | ||
| ): Promise<DocumentSymbol[] | undefined> { | ||
| try { | ||
| // Get position at the start of the code cell (skip the fence line) | ||
| const position = new Position(cellRange.start.line + 1, 0); | ||
|
|
||
| // Create virtual document for ONLY this code block (not all blocks of the language) | ||
| const vdoc = await virtualDoc(document, position, engine, VirtualDocStyle.Block); | ||
| if (!vdoc) return undefined; | ||
|
|
||
| // Get symbols from the embedded language server | ||
| return await withVirtualDocUri(vdoc, document.uri, "completion", async (uri: Uri) => { | ||
| try { | ||
| const result = await commands.executeCommand<DocumentSymbol[] | SymbolInformation[]>( | ||
| "vscode.executeDocumentSymbolProvider", | ||
| uri | ||
| ); | ||
| if (result.length === 0) return undefined; | ||
|
|
||
| if (isDocumentSymbol(result[0])) { | ||
| return unadjustSymbolRanges(result as DocumentSymbol[], vdoc.language, cellRange.start.line); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to also handle results that come back as
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, committed a change for this. |
||
| } | ||
| } catch (error) { } | ||
| }); | ||
| } catch (error) { } | ||
| } | ||
|
|
||
| /** | ||
| * Adjusts symbol ranges from virtual document to real document coordinates | ||
| */ | ||
| function unadjustSymbolRanges( | ||
| symbols: DocumentSymbol[], | ||
| language: EmbeddedLanguage, | ||
| baseLineOffset: number | ||
| ): DocumentSymbol[] { | ||
| return symbols.map(symbol => { | ||
| return { | ||
| ...symbol, | ||
| range: unadjustedRange(language, symbol.range), | ||
| selectionRange: unadjustedRange(language, symbol.selectionRange), | ||
| children: symbol.children ? unadjustSymbolRanges(symbol.children, language, baseLineOffset) : [] | ||
| }; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Creates a diagnostic handler middleware that filters out diagnostics from virtual documents | ||
| * | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
resulthere could also be nothing at all (undefinedprobably?) so it might be nicer to handle that in thisifso it doesn't show up as an error in thatcatch().Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you know it could be nothing at all? The https://code.visualstudio.com/api/references/commands seems to say that it will always resolve to a list (although it is slightly ambiguous if the type of the list is
(SymbolInformation | DocumentSymbol)[]or if itsSymbolInformation[] | DocumentSymbol[]). Google search summary says it resolves toSymbolInformation[] | DocumentSymbol[]but with no good citation. Do you know a way to figure out the return type of a command? I attempted to find the definition of the command in the vscode repo, but couldn't.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my testing I have now come across it being
undefined!