Skip to content

Commit a2a948f

Browse files
authored
Add glob pattern to notebook filters (#1519)
1 parent a34b844 commit a2a948f

6 files changed

Lines changed: 46 additions & 44 deletions

File tree

client-node-tests/src/helpers.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
AnnotatedTextEdit, TextEdit, type RelativePattern
1313
} from 'vscode-languageclient';
1414

15-
import { $GlobPattern } from 'vscode-languageclient/$test/common/diagnostic';
15+
import { matchGlobPattern } from 'vscode-languageclient/$test/common/utils/globPattern';
1616

1717
suite('Protocol Helper Tests', () => {
1818
function rangeEqual(actual: Range, expected: Range) {
@@ -208,14 +208,14 @@ suite('Protocol Helper Tests', () => {
208208
test('Relative Pattern', () => {
209209
if (process.platform === 'win32') {
210210
const pattern: RelativePattern = { baseUri: Uri.file('C:\\folder1\\folder2').toString(), pattern: '**/*.txt' };
211-
ok($GlobPattern.match(pattern, Uri.file('c:\\folder1\\folder2\\file.txt')));
212-
ok($GlobPattern.match(pattern, Uri.file('c:\\folder1\\folder2\\folder3\\file.txt')));
213-
ok(!$GlobPattern.match(pattern, Uri.file('c:\\folder1\\folder3\\file.txt')));
211+
ok(matchGlobPattern(pattern, Uri.file('c:\\folder1\\folder2\\file.txt')));
212+
ok(matchGlobPattern(pattern, Uri.file('c:\\folder1\\folder2\\folder3\\file.txt')));
213+
ok(!matchGlobPattern(pattern, Uri.file('c:\\folder1\\folder3\\file.txt')));
214214
} else {
215215
const pattern: RelativePattern = { baseUri: Uri.file('/folder1/folder2').toString(), pattern: '**/*.txt' };
216-
ok($GlobPattern.match(pattern, Uri.file('/folder1/folder2/file.txt')));
217-
ok($GlobPattern.match(pattern, Uri.file('/folder1/folder2/folder3/file.txt')));
218-
ok(!$GlobPattern.match(pattern, Uri.file('/folder1/folder3/file.txt')));
216+
ok(matchGlobPattern(pattern, Uri.file('/folder1/folder2/file.txt')));
217+
ok(matchGlobPattern(pattern, Uri.file('/folder1/folder2/folder3/file.txt')));
218+
ok(!matchGlobPattern(pattern, Uri.file('/folder1/folder3/file.txt')));
219219
}
220220
});
221221
});

client/src/common/diagnostic.ts

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
* Licensed under the MIT License. See License.txt in the project root for license information.
44
* ------------------------------------------------------------------------------------------ */
55

6-
import * as minimatch from 'minimatch';
7-
86
import {
97
Disposable, languages as Languages, window as Window, workspace as Workspace, CancellationToken, ProviderResult, Diagnostic as VDiagnostic,
108
CancellationTokenSource, TextDocument, CancellationError, Event as VEvent, EventEmitter, DiagnosticCollection, Uri, workspace, NotebookCell
@@ -15,11 +13,12 @@ import {
1513
DidSaveTextDocumentNotification, DidCloseTextDocumentNotification, LinkedMap, Touch, RAL, TextDocumentFilter, PreviousResultId,
1614
DiagnosticRegistrationOptions, DiagnosticServerCancellationData, DocumentDiagnosticParams, DocumentDiagnosticRequest, DocumentDiagnosticReportKind,
1715
WorkspaceDocumentDiagnosticReport, WorkspaceDiagnosticRequest, WorkspaceDiagnosticParams, DiagnosticOptions, DiagnosticRefreshRequest, DiagnosticTag,
18-
NotebookDocumentSyncRegistrationType,
19-
type GlobPattern
16+
NotebookDocumentSyncRegistrationType
2017
} from 'vscode-languageserver-protocol';
2118

2219
import { generateUuid } from './utils/uuid';
20+
import { matchGlobPattern } from './utils/globPattern';
21+
2322
import {
2423
TextDocumentLanguageFeature, FeatureClient, LSPCancellationError, type TabsModel
2524
} from './features';
@@ -756,27 +755,6 @@ class BackgroundScheduler implements Disposable {
756755
}
757756
}
758757

759-
export namespace $GlobPattern {
760-
export function match(pattern: GlobPattern, resource: Uri): boolean {
761-
let miniMatchPattern: string;
762-
if (typeof pattern === 'string') {
763-
miniMatchPattern = pattern.replace(/\\/g, '/');
764-
} else {
765-
try {
766-
const baseUri = Uri.parse(typeof pattern.baseUri === 'string' ? pattern.baseUri : pattern.baseUri.uri);
767-
miniMatchPattern = baseUri.with({ path: baseUri.path + '/' + pattern.pattern }).fsPath.replace(/\\/g, '/');
768-
} catch (error) {
769-
return false;
770-
}
771-
}
772-
const matcher = new minimatch.Minimatch(miniMatchPattern, { noext: true });
773-
if (!matcher.makeRe()) {
774-
return false;
775-
}
776-
return matcher.match(resource.fsPath);
777-
}
778-
}
779-
780758
class DiagnosticFeatureProviderImpl implements DiagnosticProviderShape {
781759

782760
public readonly disposable: Disposable;
@@ -802,7 +780,7 @@ class DiagnosticFeatureProviderImpl implements DiagnosticProviderShape {
802780
if (filter.scheme !== undefined && filter.scheme !== '*' && filter.scheme !== resource.scheme) {
803781
return false;
804782
}
805-
if (filter.pattern !== undefined && !$GlobPattern.match(filter.pattern, resource)) {
783+
if (filter.pattern !== undefined && !matchGlobPattern(filter.pattern, resource)) {
806784
return false;
807785
}
808786
return true;

client/src/common/notebook.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
* ------------------------------------------------------------------------------------------ */
55

66
import * as vscode from 'vscode';
7-
import * as minimatch from 'minimatch';
87

98
import * as proto from 'vscode-languageserver-protocol';
109
import {
@@ -18,6 +17,7 @@ import * as Is from './utils/is';
1817
import * as _c2p from './codeConverter';
1918
import * as _p2c from './protocolConverter';
2019
import { DynamicFeature, FeatureClient, RegistrationData, FeatureState } from './features';
20+
import { matchGlobPattern } from './utils/globPattern';
2121

2222

2323
function ensure<T, K extends keyof T>(target: T, key: K): T[K] {
@@ -285,11 +285,7 @@ namespace $NotebookDocumentFilter {
285285
return false;
286286
}
287287
if (filter.pattern !== undefined) {
288-
const matcher = new minimatch.Minimatch(filter.pattern, { noext: true });
289-
if (!matcher.makeRe()) {
290-
return false;
291-
}
292-
if (!matcher.match(uri.fsPath)) {
288+
if (!matchGlobPattern(filter.pattern, uri)) {
293289
return false;
294290
}
295291
}
@@ -316,7 +312,7 @@ namespace $NotebookDocumentSyncOptions {
316312
return result;
317313
}
318314

319-
function asDocumentFilter(notebookType: string, scheme: string | undefined, pattern: string | undefined, language: string | undefined): proto.NotebookCellTextDocumentFilter {
315+
function asDocumentFilter(notebookType: string, scheme: string | undefined, pattern: proto.GlobPattern | undefined, language: string | undefined): proto.NotebookCellTextDocumentFilter {
320316
return scheme === undefined && pattern === undefined
321317
? { notebook: notebookType, language }
322318
: { notebook: { notebookType, scheme, pattern }, language };

client/src/common/protocolConverter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ export function createConverter(
303303
result.push({notebookType: filter.notebook, language: filter.language});
304304
} else {
305305
const notebookType = filter.notebook.notebookType ?? '*';
306-
result.push({ notebookType: notebookType, scheme: filter.notebook.scheme, pattern: filter.notebook.pattern, language: filter.language });
306+
result.push({ notebookType: notebookType, scheme: filter.notebook.scheme, pattern: asGlobPattern(filter.notebook.pattern), language: filter.language });
307307
}
308308
} else if (TextDocumentFilter.is(filter)) {
309309
result.push({ language: filter.language, scheme: filter.scheme, pattern: asGlobPattern(filter.pattern) });
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/* --------------------------------------------------------------------------------------------
2+
* Copyright (c) Microsoft Corporation. All rights reserved.
3+
* Licensed under the MIT License. See License.txt in the project root for license information.
4+
* ------------------------------------------------------------------------------------------ */
5+
6+
import * as minimatch from 'minimatch';
7+
8+
import { Uri } from 'vscode';
9+
import type { GlobPattern } from 'vscode-languageserver-protocol';
10+
11+
export function matchGlobPattern(pattern: GlobPattern, resource: Uri): boolean {
12+
let miniMatchPattern: string;
13+
if (typeof pattern === 'string') {
14+
miniMatchPattern = pattern.replace(/\\/g, '/');
15+
} else {
16+
try {
17+
const baseUri = Uri.parse(typeof pattern.baseUri === 'string' ? pattern.baseUri : pattern.baseUri.uri);
18+
miniMatchPattern = baseUri.with({ path: baseUri.path + '/' + pattern.pattern }).fsPath.replace(/\\/g, '/');
19+
} catch (error) {
20+
return false;
21+
}
22+
}
23+
const matcher = new minimatch.Minimatch(miniMatchPattern, { noext: true });
24+
if (!matcher.makeRe()) {
25+
return false;
26+
}
27+
return matcher.match(resource.fsPath);
28+
}

protocol/src/common/protocol.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ export type NotebookDocumentFilterNotebookType = {
250250
/**
251251
* A glob pattern.
252252
*/
253-
pattern?: string;
253+
pattern?: GlobPattern;
254254
};
255255

256256
/**
@@ -272,7 +272,7 @@ export type NotebookDocumentFilterScheme = {
272272
/**
273273
* A glob pattern.
274274
*/
275-
pattern?: string;
275+
pattern?: GlobPattern;
276276
};
277277

278278
/**
@@ -294,7 +294,7 @@ export type NotebookDocumentFilterPattern = {
294294
/**
295295
* A glob pattern.
296296
*/
297-
pattern: string;
297+
pattern: GlobPattern;
298298
};
299299

300300
/**

0 commit comments

Comments
 (0)