-
Notifications
You must be signed in to change notification settings - Fork 55
Add file linking and autocomplete for _quarto.yml file using vscode extension #906
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
Merged
Merged
Changes from 3 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
5262119
Add document link provider for _quarto.yml files in vscode extension
EmilHvitfeldt 2c03e13
add quarto yaml file completions
EmilHvitfeldt ce064ce
activateYamlCompletions -> activateYamlFilepathCompletions
EmilHvitfeldt b3eb2e2
update copyright years
EmilHvitfeldt 6c26d5e
activate vscode yaml providers on _quarto.ya?ml instead of on yaml files
EmilHvitfeldt 20be497
document yaml providers
EmilHvitfeldt e9981bb
add tests for providers
EmilHvitfeldt 2254fdd
update changelog
EmilHvitfeldt 5c12b78
Merge branch 'main' into quarto-yml-vscode
vezwork 8a88d82
Update changelog
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
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 |
|---|---|---|
| @@ -0,0 +1,125 @@ | ||
| /* | ||
| * yaml-filepath-completions.ts | ||
| * | ||
| * Copyright (C) 2022 by Posit Software, PBC | ||
| * | ||
| * Unless you have received this program directly from Posit Software pursuant | ||
| * to the terms of a commercial license agreement with Posit Software, then | ||
| * this program is licensed to you under the terms of version 3 of the | ||
| * GNU Affero General Public License. This program is distributed WITHOUT | ||
| * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, | ||
| * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the | ||
| * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. | ||
| * | ||
| */ | ||
|
|
||
| import * as path from "path"; | ||
|
|
||
| import { glob } from "glob"; | ||
|
|
||
| import { | ||
| CancellationToken, | ||
| CompletionContext, | ||
| CompletionItem, | ||
| CompletionItemKind, | ||
| CompletionItemProvider, | ||
| ExtensionContext, | ||
| languages, | ||
| Position, | ||
| Range, | ||
| TextDocument, | ||
| workspace, | ||
| } from "vscode"; | ||
|
|
||
| import { isQuartoYaml } from "../core/doc"; | ||
|
|
||
| const FILE_EXTENSIONS = ['qmd', 'scss', 'css', 'html', 'js', 'bib', 'tex', 'md']; | ||
| const IGNORE_PATTERNS = ['.git', 'node_modules', '_site', '_freeze', '.quarto']; | ||
|
|
||
| export function activateYamlFilepathCompletions(context: ExtensionContext) { | ||
| const config = workspace.getConfiguration("quarto"); | ||
|
|
||
| if (config.get<boolean>("yaml.filepathCompletions.enabled", true)) { | ||
| context.subscriptions.push( | ||
| languages.registerCompletionItemProvider( | ||
| { language: "yaml", scheme: "file" }, | ||
| new QuartoYamlFilepathCompletionProvider(), | ||
| "/", "." // Trigger on path separators | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class QuartoYamlFilepathCompletionProvider implements CompletionItemProvider { | ||
| async provideCompletionItems( | ||
| document: TextDocument, | ||
| position: Position, | ||
| _token: CancellationToken, | ||
| _context: CompletionContext | ||
| ): Promise<CompletionItem[]> { | ||
| if (!isQuartoYaml(document)) { | ||
| return []; | ||
| } | ||
|
|
||
| const line = document.lineAt(position).text; | ||
| const linePrefix = line.substring(0, position.character); | ||
|
|
||
| // Only provide completions after ': ' or '- ' | ||
| if (!this.shouldProvideCompletions(linePrefix)) { | ||
| return []; | ||
| } | ||
|
|
||
| const documentDir = path.dirname(document.uri.fsPath); | ||
| const projectFiles = await getProjectFiles(documentDir); | ||
|
|
||
| // Get current input to filter completions | ||
| const currentInput = this.getCurrentInput(linePrefix); | ||
|
|
||
| return projectFiles | ||
| .filter(file => file.toLowerCase().includes(currentInput.toLowerCase())) | ||
| .map(file => { | ||
| const item = new CompletionItem(file, CompletionItemKind.File); | ||
| item.detail = "Quarto project file"; | ||
| item.insertText = file; | ||
|
|
||
| // If there's existing input, replace it | ||
| if (currentInput) { | ||
| const startPos = position.character - currentInput.length; | ||
| item.range = new Range( | ||
| new Position(position.line, startPos), | ||
| position | ||
| ); | ||
| } | ||
|
|
||
| return item; | ||
| }); | ||
| } | ||
|
|
||
| private shouldProvideCompletions(linePrefix: string): boolean { | ||
| // Check if we're in a position where file path completions make sense | ||
| // After ': ' for key-value pairs or after '- ' for list items | ||
| return /(?::\s+|^\s*-\s+)\S*$/.test(linePrefix); | ||
| } | ||
|
|
||
| private getCurrentInput(linePrefix: string): string { | ||
| // Extract the current partial input after ': ' or '- ' | ||
| const match = linePrefix.match(/(?::\s+|^\s*-\s+)(\S*)$/); | ||
| return match ? match[1] : ""; | ||
| } | ||
| } | ||
|
|
||
| async function getProjectFiles(projectDir: string): Promise<string[]> { | ||
| const extensionPattern = `**/*.{${FILE_EXTENSIONS.join(',')}}`; | ||
|
|
||
| try { | ||
| const files = await glob(extensionPattern, { | ||
| cwd: projectDir, | ||
| ignore: IGNORE_PATTERNS.map(p => `**/${p}/**`), | ||
| nodir: true, | ||
| }); | ||
|
|
||
| return files.sort(); | ||
| } catch { | ||
| return []; | ||
| } | ||
| } | ||
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 |
|---|---|---|
| @@ -0,0 +1,117 @@ | ||
| /* | ||
| * yaml-links.ts | ||
| * | ||
| * Copyright (C) 2022 by Posit Software, PBC | ||
| * | ||
| * Unless you have received this program directly from Posit Software pursuant | ||
| * to the terms of a commercial license agreement with Posit Software, then | ||
| * this program is licensed to you under the terms of version 3 of the | ||
| * GNU Affero General Public License. This program is distributed WITHOUT | ||
| * ANY EXPRESS OR IMPLIED WARRANTY, INCLUDING THOSE OF NON-INFRINGEMENT, | ||
| * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. Please refer to the | ||
| * AGPL (http://www.gnu.org/licenses/agpl-3.0.txt) for more details. | ||
| * | ||
| */ | ||
|
|
||
| import path from "node:path"; | ||
| import fs from "node:fs"; | ||
|
|
||
| import { | ||
| CancellationToken, | ||
| DocumentLink, | ||
| DocumentLinkProvider, | ||
| ExtensionContext, | ||
| languages, | ||
| Position, | ||
| Range, | ||
| TextDocument, | ||
| Uri, | ||
| workspace, | ||
| } from "vscode"; | ||
|
|
||
| import { isQuartoYaml } from "../core/doc"; | ||
|
|
||
| const FILE_EXTENSIONS = ['qmd', 'scss', 'css', 'html', 'js', 'bib', 'tex', 'md']; | ||
| const IGNORE_PATTERNS = ['.git', 'node_modules', '_site', '_freeze', '.quarto']; | ||
|
|
||
| export function activateYamlLinks(context: ExtensionContext) { | ||
| const config = workspace.getConfiguration("quarto"); | ||
|
|
||
| if (config.get<boolean>("yaml.documentLinks.enabled", true)) { | ||
| context.subscriptions.push( | ||
| languages.registerDocumentLinkProvider( | ||
| { language: "yaml", scheme: "file" }, | ||
| new QuartoYamlLinkProvider() | ||
| ) | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| class QuartoYamlLinkProvider implements DocumentLinkProvider { | ||
| provideDocumentLinks( | ||
| document: TextDocument, | ||
| _token: CancellationToken | ||
| ): DocumentLink[] { | ||
| if (!isQuartoYaml(document)) { | ||
| return []; | ||
| } | ||
|
|
||
| const links: DocumentLink[] = []; | ||
| const text = document.getText(); | ||
| const lines = text.split('\n'); | ||
| const documentDir = path.dirname(document.uri.fsPath); | ||
|
|
||
| for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) { | ||
| const line = lines[lineIndex]; | ||
| const lineLinks = this.findLinksInLine(line, lineIndex, documentDir); | ||
| links.push(...lineLinks); | ||
| } | ||
|
|
||
| return links; | ||
| } | ||
|
|
||
| private findLinksInLine( | ||
| line: string, | ||
| lineIndex: number, | ||
| documentDir: string | ||
| ): DocumentLink[] { | ||
| const links: DocumentLink[] = []; | ||
|
|
||
| // Match file paths in YAML values | ||
| // Pattern: looks for paths after ': ' or '- ' that end with known extensions | ||
| const extensionPattern = FILE_EXTENSIONS.join('|'); | ||
| const regex = new RegExp( | ||
| `(?:^\\s*-\\s*|:\\s*)([\\w./-]+\\.(?:${extensionPattern}))`, | ||
| 'gi' | ||
| ); | ||
|
|
||
| let match; | ||
| while ((match = regex.exec(line)) !== null) { | ||
| const filePath = match[1]; | ||
| const startIndex = match.index + match[0].length - filePath.length; | ||
| const endIndex = startIndex + filePath.length; | ||
|
|
||
| // Resolve the full path | ||
| const fullPath = path.isAbsolute(filePath) | ||
| ? filePath | ||
| : path.resolve(documentDir, filePath); | ||
|
|
||
| // Check if the file exists | ||
| if (fs.existsSync(fullPath)) { | ||
| const range = new Range( | ||
| new Position(lineIndex, startIndex), | ||
| new Position(lineIndex, endIndex) | ||
| ); | ||
|
|
||
| const link = new DocumentLink( | ||
| range, | ||
| Uri.file(fullPath) | ||
| ); | ||
| link.tooltip = `Open ${filePath}`; | ||
| links.push(link); | ||
| } | ||
| } | ||
|
|
||
| return links; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.