Skip to content

Commit 4765f12

Browse files
authored
recognize _brand.ya?ml as part of a Quarto project (#565)
* recognize _brand.ya?ml as part of a Quarto project * only handle brand.yml in sufficiently recent Quarto versions
1 parent 20fb64c commit 4765f12

File tree

2 files changed

+53
-46
lines changed

2 files changed

+53
-46
lines changed

apps/vscode/src/lsp/client.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import { getHover, getSignatureHelpHover } from "../core/hover";
6464
import { imageHover } from "../providers/hover-image";
6565
import { LspInitializationOptions, QuartoContext } from "quarto-core";
6666
import { extensionHost } from "../host";
67+
import semver from "semver";
6768

6869
let client: LanguageClient;
6970

@@ -110,19 +111,24 @@ export async function activateLsp(
110111
middleware.provideSignatureHelp = embeddedSignatureHelpProvider(engine);
111112
}
112113
extensionHost().registerStatementRangeProvider(engine);
113-
114+
114115
// create client options
115-
const initializationOptions : LspInitializationOptions = {
116+
const initializationOptions: LspInitializationOptions = {
116117
quartoBinPath: quartoContext.binPath
117118
};
119+
120+
const documentSelectorPattern = semver.gte(quartoContext.version, "1.6.24") ?
121+
"**/_{brand,quarto,metadata,extension}*.{yml,yaml}" :
122+
"**/_{quarto,metadata,extension}*.{yml,yaml}";
123+
118124
const clientOptions: LanguageClientOptions = {
119125
initializationOptions,
120126
documentSelector: [
121127
{ scheme: "*", language: "quarto" },
122128
{
123129
scheme: "*",
124130
language: "yaml",
125-
pattern: "**/_{quarto,metadata,extension}*.{yml,yaml}",
131+
pattern: documentSelectorPattern,
126132
},
127133
],
128134
middleware,
@@ -284,7 +290,7 @@ function embeddedGoToDefinitionProvider(engine: MarkdownEngine) {
284290
);
285291
const resolveLocation = (location: Location) => {
286292
if (isLanguageVirtualDoc(vdoc.language, location.uri) ||
287-
location.uri.toString() === vdocUri.uri.toString()) {
293+
location.uri.toString() === vdocUri.uri.toString()) {
288294
return new Location(
289295
document.uri,
290296
unadjustedRange(vdoc.language, location.range)
@@ -295,7 +301,7 @@ function embeddedGoToDefinitionProvider(engine: MarkdownEngine) {
295301
};
296302
const resolveLocationLink = (location: LocationLink) => {
297303
if (isLanguageVirtualDoc(vdoc.language, location.targetUri) ||
298-
location.targetUri.toString() === vdocUri.uri.toString()) {
304+
location.targetUri.toString() === vdocUri.uri.toString()) {
299305
const locationLink: LocationLink = {
300306
targetRange: unadjustedRange(vdoc.language, location.targetRange),
301307
originSelectionRange: location.originSelectionRange

packages/quarto-core/src/document.ts

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* document.ts
33
*
4-
* Copyright (C) 2023 by Posit Software, PBC
4+
* Copyright (C) 2023-2024 by Posit Software, PBC
55
* Copyright (c) Microsoft Corporation. All rights reserved.
66
*
77
* Unless you have received this program directly from Posit Software pursuant
@@ -22,50 +22,50 @@ import { makeRange } from 'quarto-core';
2222
* A document in the workspace.
2323
*/
2424
export interface Document {
25-
/**
26-
* The uri of the document, as a string.
27-
*/
28-
readonly uri: string;
29-
30-
/**
31-
* The uri of the document, as a URI.
32-
*/
33-
readonly $uri?: URI;
34-
35-
/**
36-
* The lanugageId of the document
37-
*/
38-
readonly languageId : string | undefined;
39-
40-
/**
41-
* Version number of the document's content.
42-
*/
43-
readonly version: number;
44-
45-
/**
46-
* The total number of lines in the document.
47-
*/
48-
readonly lineCount: number;
49-
50-
/**
51-
* Get text contents of the document.
52-
*
53-
* @param range Optional range to get the text of. If not specified, the entire document content is returned.
54-
*/
55-
getText(range?: Range): string;
56-
57-
/**
58-
* Converts an offset in the document into a {@link Position position}.
59-
*/
60-
positionAt(offset: number): Position;
25+
/**
26+
* The uri of the document, as a string.
27+
*/
28+
readonly uri: string;
29+
30+
/**
31+
* The uri of the document, as a URI.
32+
*/
33+
readonly $uri?: URI;
34+
35+
/**
36+
* The lanugageId of the document
37+
*/
38+
readonly languageId: string | undefined;
39+
40+
/**
41+
* Version number of the document's content.
42+
*/
43+
readonly version: number;
44+
45+
/**
46+
* The total number of lines in the document.
47+
*/
48+
readonly lineCount: number;
49+
50+
/**
51+
* Get text contents of the document.
52+
*
53+
* @param range Optional range to get the text of. If not specified, the entire document content is returned.
54+
*/
55+
getText(range?: Range): string;
56+
57+
/**
58+
* Converts an offset in the document into a {@link Position position}.
59+
*/
60+
positionAt(offset: number): Position;
6161
}
6262

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

6767
export function getDocUri(doc: Document): URI {
68-
return doc.$uri ?? URI.parse(doc.uri);
68+
return doc.$uri ?? URI.parse(doc.uri);
6969
}
7070

7171

@@ -100,6 +100,7 @@ export function isQuartoYaml(doc: Document) {
100100
return (
101101
doc.languageId === kYamlLanguageId &&
102102
(doc.uri.match(/_quarto(-.*?)?\.ya?ml$/) ||
103+
doc.uri.match(/_brand\.ya?ml$/) ||
103104
doc.uri.match(/_metadata\.ya?ml$/) ||
104105
doc.uri.match(/_extension\.ya?ml$/))
105106
);
@@ -113,7 +114,7 @@ const kRegExYAML =
113114
/(^)(---[ \t]*[\r\n]+(?![ \t]*[\r\n]+)[\W\w]*?[\r\n]+(?:---|\.\.\.))([ \t]*)$/gm;
114115

115116
export function isQuartoDocWithFormat(doc: Document | string, format: string) {
116-
if (typeof(doc) !== "string") {
117+
if (typeof (doc) !== "string") {
117118
if (isQuartoDoc(doc)) {
118119
doc = doc.getText();
119120
} else {
@@ -125,7 +126,7 @@ export function isQuartoDocWithFormat(doc: Document | string, format: string) {
125126
if (match) {
126127
const yaml = match[0];
127128
return (
128-
!!yaml.match(new RegExp("^format:\\s+" + format + "\\s*$","gm")) ||
129+
!!yaml.match(new RegExp("^format:\\s+" + format + "\\s*$", "gm")) ||
129130
!!yaml.match(new RegExp("^[ \\t]*" + format + ":\\s*(default)?\\s*$", "gm"))
130131
);
131132
}

0 commit comments

Comments
 (0)