Skip to content

recognize _brand.ya?ml as part of a Quarto project #565

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 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 11 additions & 5 deletions apps/vscode/src/lsp/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ import { getHover, getSignatureHelpHover } from "../core/hover";
import { imageHover } from "../providers/hover-image";
import { LspInitializationOptions, QuartoContext } from "quarto-core";
import { extensionHost } from "../host";
import semver from "semver";

let client: LanguageClient;

Expand Down Expand Up @@ -110,19 +111,24 @@ export async function activateLsp(
middleware.provideSignatureHelp = embeddedSignatureHelpProvider(engine);
}
extensionHost().registerStatementRangeProvider(engine);

// create client options
const initializationOptions : LspInitializationOptions = {
const initializationOptions: LspInitializationOptions = {
quartoBinPath: quartoContext.binPath
};

const documentSelectorPattern = semver.gte(quartoContext.version, "1.6.24") ?
"**/_{brand,quarto,metadata,extension}*.{yml,yaml}" :
"**/_{quarto,metadata,extension}*.{yml,yaml}";

const clientOptions: LanguageClientOptions = {
initializationOptions,
documentSelector: [
{ scheme: "*", language: "quarto" },
{
scheme: "*",
language: "yaml",
pattern: "**/_{quarto,metadata,extension}*.{yml,yaml}",
pattern: documentSelectorPattern,
},
],
middleware,
Expand Down Expand Up @@ -284,7 +290,7 @@ function embeddedGoToDefinitionProvider(engine: MarkdownEngine) {
);
const resolveLocation = (location: Location) => {
if (isLanguageVirtualDoc(vdoc.language, location.uri) ||
location.uri.toString() === vdocUri.uri.toString()) {
location.uri.toString() === vdocUri.uri.toString()) {
return new Location(
document.uri,
unadjustedRange(vdoc.language, location.range)
Expand All @@ -295,7 +301,7 @@ function embeddedGoToDefinitionProvider(engine: MarkdownEngine) {
};
const resolveLocationLink = (location: LocationLink) => {
if (isLanguageVirtualDoc(vdoc.language, location.targetUri) ||
location.targetUri.toString() === vdocUri.uri.toString()) {
location.targetUri.toString() === vdocUri.uri.toString()) {
const locationLink: LocationLink = {
targetRange: unadjustedRange(vdoc.language, location.targetRange),
originSelectionRange: location.originSelectionRange
Expand Down
83 changes: 42 additions & 41 deletions packages/quarto-core/src/document.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* document.ts
*
* Copyright (C) 2023 by Posit Software, PBC
* Copyright (C) 2023-2024 by Posit Software, PBC
* Copyright (c) Microsoft Corporation. All rights reserved.
*
* Unless you have received this program directly from Posit Software pursuant
Expand All @@ -22,50 +22,50 @@ import { makeRange } from 'quarto-core';
* A document in the workspace.
*/
export interface Document {
/**
* The uri of the document, as a string.
*/
readonly uri: string;

/**
* The uri of the document, as a URI.
*/
readonly $uri?: URI;
/**
* The lanugageId of the document
*/
readonly languageId : string | undefined;

/**
* Version number of the document's content.
*/
readonly version: number;

/**
* The total number of lines in the document.
*/
readonly lineCount: number;

/**
* Get text contents of the document.
*
* @param range Optional range to get the text of. If not specified, the entire document content is returned.
*/
getText(range?: Range): string;

/**
* Converts an offset in the document into a {@link Position position}.
*/
positionAt(offset: number): Position;
/**
* The uri of the document, as a string.
*/
readonly uri: string;

/**
* The uri of the document, as a URI.
*/
readonly $uri?: URI;

/**
* The lanugageId of the document
*/
readonly languageId: string | undefined;

/**
* Version number of the document's content.
*/
readonly version: number;

/**
* The total number of lines in the document.
*/
readonly lineCount: number;

/**
* Get text contents of the document.
*
* @param range Optional range to get the text of. If not specified, the entire document content is returned.
*/
getText(range?: Range): string;

/**
* Converts an offset in the document into a {@link Position position}.
*/
positionAt(offset: number): Position;
}

export function getLine(doc: Document, line: number): string {
return doc.getText(makeRange(line, 0, line, Number.MAX_VALUE)).replace(/\r?\n$/, '');
return doc.getText(makeRange(line, 0, line, Number.MAX_VALUE)).replace(/\r?\n$/, '');
}

export function getDocUri(doc: Document): URI {
return doc.$uri ?? URI.parse(doc.uri);
return doc.$uri ?? URI.parse(doc.uri);
}


Expand Down Expand Up @@ -100,6 +100,7 @@ export function isQuartoYaml(doc: Document) {
return (
doc.languageId === kYamlLanguageId &&
(doc.uri.match(/_quarto(-.*?)?\.ya?ml$/) ||
doc.uri.match(/_brand\.ya?ml$/) ||
doc.uri.match(/_metadata\.ya?ml$/) ||
doc.uri.match(/_extension\.ya?ml$/))
);
Expand All @@ -113,7 +114,7 @@ const kRegExYAML =
/(^)(---[ \t]*[\r\n]+(?![ \t]*[\r\n]+)[\W\w]*?[\r\n]+(?:---|\.\.\.))([ \t]*)$/gm;

export function isQuartoDocWithFormat(doc: Document | string, format: string) {
if (typeof(doc) !== "string") {
if (typeof (doc) !== "string") {
if (isQuartoDoc(doc)) {
doc = doc.getText();
} else {
Expand All @@ -125,7 +126,7 @@ export function isQuartoDocWithFormat(doc: Document | string, format: string) {
if (match) {
const yaml = match[0];
return (
!!yaml.match(new RegExp("^format:\\s+" + format + "\\s*$","gm")) ||
!!yaml.match(new RegExp("^format:\\s+" + format + "\\s*$", "gm")) ||
!!yaml.match(new RegExp("^[ \\t]*" + format + ":\\s*(default)?\\s*$", "gm"))
);
}
Expand Down