From 715085f954ce1c5b933b054a9278d7cbadbdf8bc Mon Sep 17 00:00:00 2001 From: Misode Date: Sun, 29 Dec 2024 21:54:35 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Exclude=20patterns=20using=20microm?= =?UTF-8?q?atch?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 36 +++++++++++++++++++++-- packages/core/package.json | 2 ++ packages/core/src/service/CacheService.ts | 4 +++ packages/core/src/service/Config.ts | 2 +- packages/core/src/service/Project.ts | 31 +++++++++++++++++-- 5 files changed, 69 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7646fc477..da3099261 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1852,6 +1852,12 @@ "integrity": "sha512-EDKtLYNMKrig22jEvhXq8TBFyFgVNSPmDF2b9UzJ7+eylPqdZVo17PCUMkn1jP6/1A/0u78VqYC6VrX6b8pDWA==", "dev": true }, + "node_modules/@types/braces": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/braces/-/braces-3.0.4.tgz", + "integrity": "sha512-0WR3b8eaISjEW7RpZnclONaLFDf7buaowRHdqLp4vLj54AsSAYWfh3DRbfiYJY9XDxMgx1B4sE1Afw2PGpuHOA==", + "dev": true + }, "node_modules/@types/decompress": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/@types/decompress/-/decompress-4.2.3.tgz", @@ -1928,6 +1934,15 @@ "integrity": "sha512-wbw+IDRw/xY/RGy+BL6f4Eey4jsUgHQrMuA4Qj0CSG3x/7C2Oc57pmRoM2z3M4DkylWRz+G1pfX06sCXQm0J+w==", "dev": true }, + "node_modules/@types/micromatch": { + "version": "4.0.9", + "resolved": "https://registry.npmjs.org/@types/micromatch/-/micromatch-4.0.9.tgz", + "integrity": "sha512-7V+8ncr22h4UoYRLnLXSpTxjQrNUXtWHGeMPRJt1nULXI57G9bIcpyrHlmrQ7QK24EyyuXvYcSSWAM8GA9nqCg==", + "dev": true, + "dependencies": { + "@types/braces": "*" + } + }, "node_modules/@types/mocha": { "version": "10.0.1", "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.1.tgz", @@ -9074,7 +9089,6 @@ "version": "4.0.8", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", - "dev": true, "dependencies": { "braces": "^3.0.3", "picomatch": "^2.3.1" @@ -13660,6 +13674,7 @@ "chokidar": "^3.5.2", "decompress": "^4.2.1", "follow-redirects": "^1.14.8", + "micromatch": "^4.0.8", "pako": "^2.0.4", "rfdc": "^1.3.0", "vscode-languageserver-textdocument": "^1.0.4", @@ -13668,6 +13683,7 @@ "devDependencies": { "@types/decompress": "^4.2.3", "@types/follow-redirects": "^1.14.1", + "@types/micromatch": "^4.0.9", "@types/pako": "^2.0.0", "@types/whatwg-url": "^11.0.4" } @@ -15116,6 +15132,7 @@ "requires": { "@types/decompress": "^4.2.3", "@types/follow-redirects": "^1.14.1", + "@types/micromatch": "^4.0.9", "@types/pako": "^2.0.0", "@types/whatwg-url": "^11.0.4", "base64-arraybuffer": "^1.0.2", @@ -15123,6 +15140,7 @@ "chokidar": "^3.5.2", "decompress": "^4.2.1", "follow-redirects": "^1.14.8", + "micromatch": "^4.0.8", "pako": "^2.0.4", "rfdc": "^1.3.0", "vscode-languageserver-textdocument": "^1.0.4", @@ -15304,6 +15322,12 @@ "integrity": "sha512-EDKtLYNMKrig22jEvhXq8TBFyFgVNSPmDF2b9UzJ7+eylPqdZVo17PCUMkn1jP6/1A/0u78VqYC6VrX6b8pDWA==", "dev": true }, + "@types/braces": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/@types/braces/-/braces-3.0.4.tgz", + "integrity": "sha512-0WR3b8eaISjEW7RpZnclONaLFDf7buaowRHdqLp4vLj54AsSAYWfh3DRbfiYJY9XDxMgx1B4sE1Afw2PGpuHOA==", + "dev": true + }, "@types/decompress": { "version": "4.2.3", "resolved": "https://registry.npmjs.org/@types/decompress/-/decompress-4.2.3.tgz", @@ -15380,6 +15404,15 @@ "integrity": "sha512-wbw+IDRw/xY/RGy+BL6f4Eey4jsUgHQrMuA4Qj0CSG3x/7C2Oc57pmRoM2z3M4DkylWRz+G1pfX06sCXQm0J+w==", "dev": true }, + "@types/micromatch": { + "version": "4.0.9", + "resolved": "https://registry.npmjs.org/@types/micromatch/-/micromatch-4.0.9.tgz", + "integrity": "sha512-7V+8ncr22h4UoYRLnLXSpTxjQrNUXtWHGeMPRJt1nULXI57G9bIcpyrHlmrQ7QK24EyyuXvYcSSWAM8GA9nqCg==", + "dev": true, + "requires": { + "@types/braces": "*" + } + }, "@types/mocha": { "version": "10.0.1", "resolved": "https://registry.npmjs.org/@types/mocha/-/mocha-10.0.1.tgz", @@ -20612,7 +20645,6 @@ "version": "4.0.8", "resolved": "https://registry.npmjs.org/micromatch/-/micromatch-4.0.8.tgz", "integrity": "sha512-PXwfBhYu0hBCPw8Dn0E+WDYb7af3dSLVWKi3HGv84IdF4TyFoC0ysxFd0Goxw7nSv4T/PzEJQxsYsEiFCKo2BA==", - "dev": true, "requires": { "braces": "^3.0.3", "picomatch": "^2.3.1" diff --git a/packages/core/package.json b/packages/core/package.json index 5fbde3315..cc6c43634 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -19,6 +19,7 @@ "chokidar": "^3.5.2", "decompress": "^4.2.1", "follow-redirects": "^1.14.8", + "micromatch": "^4.0.8", "pako": "^2.0.4", "rfdc": "^1.3.0", "vscode-languageserver-textdocument": "^1.0.4", @@ -27,6 +28,7 @@ "devDependencies": { "@types/decompress": "^4.2.3", "@types/follow-redirects": "^1.14.1", + "@types/micromatch": "^4.0.9", "@types/pako": "^2.0.0", "@types/whatwg-url": "^11.0.4" }, diff --git a/packages/core/src/service/CacheService.ts b/packages/core/src/service/CacheService.ts index f16248a4b..2208c8c31 100644 --- a/packages/core/src/service/CacheService.ts +++ b/packages/core/src/service/CacheService.ts @@ -174,6 +174,10 @@ export class CacheService { ans.unchangedFiles.push(uri) continue } + if (this.project.shouldExclude(uri)) { + ans.removedFiles.push(uri) + continue + } try { const hash = await this.project.fs.hash(uri) diff --git a/packages/core/src/service/Config.ts b/packages/core/src/service/Config.ts index be4bd90e1..3c10bcfa9 100644 --- a/packages/core/src/service/Config.ts +++ b/packages/core/src/service/Config.ts @@ -336,7 +336,7 @@ export const VanillaConfig: Config = { env: { dataSource: 'GitHub', dependencies: ['@vanilla-datapack', '@vanilla-resourcepack', '@vanilla-mcdoc'], - exclude: [], + exclude: ['.*/**'], customResources: {}, feature: { codeActions: true, diff --git a/packages/core/src/service/Project.ts b/packages/core/src/service/Project.ts index c6b8d802b..4f9680219 100644 --- a/packages/core/src/service/Project.ts +++ b/packages/core/src/service/Project.ts @@ -1,5 +1,4 @@ -import type { Ignore } from 'ignore' -import ignore from 'ignore' +import * as micromatch from 'micromatch' import type { TextDocumentContentChangeEvent } from 'vscode-languageserver-textdocument' import { TextDocument } from 'vscode-languageserver-textdocument' import type { ExternalEventEmitter, Externals, FsWatcher, IntervalId } from '../common/index.js' @@ -158,7 +157,6 @@ export type ProjectData = Pick< */ export class Project implements ExternalEventEmitter { private static readonly RootSuffix = '/pack.mcmeta' - private static readonly GitIgnore = '.gitignore' /** Prevent circular binding. */ readonly #bindingInProgressUris = new Set() @@ -494,15 +492,24 @@ export class Project implements ExternalEventEmitter { this.#watcherReady = true resolve() }).on('add', (uri) => { + if (this.shouldExclude(uri)) { + return + } this.#watchedFiles.add(uri) if (this.#watcherReady) { this.emit('fileCreated', { uri }) } }).on('change', (uri) => { + if (this.shouldExclude(uri)) { + return + } if (this.#watcherReady) { this.emit('fileModified', { uri }) } }).on('unlink', (uri) => { + if (this.shouldExclude(uri)) { + return + } this.#watchedFiles.delete(uri) if (this.#watcherReady) { this.emit('fileDeleted', { uri }) @@ -868,6 +875,9 @@ export class Project implements ExternalEventEmitter { if (!fileUtil.isFileUri(uri)) { return // We only accept `file:` scheme for client-managed URIs. } + if (this.shouldExclude(uri)) { + return + } const doc = TextDocument.create(uri, languageID, version, content) const node = this.parse(doc) this.#clientManagedUris.add(uri) @@ -892,6 +902,9 @@ export class Project implements ExternalEventEmitter { if (!fileUtil.isFileUri(uri)) { return // We only accept `file:` scheme for client-managed URIs. } + if (this.shouldExclude(uri)) { + return + } const doc = this.#clientManagedDocAndNodes.get(uri)?.doc if (!doc) { throw new Error( @@ -954,6 +967,18 @@ export class Project implements ExternalEventEmitter { } } + public shouldExclude(uri: string): boolean { + if (this.config.env.exclude.length === 0) { + return false + } + for (const rel of fileUtil.getRels(uri, this.projectRoots)) { + if (micromatch.any(rel, this.config.env.exclude)) { + return true + } + } + return false + } + private tryClearingCache(uri: string): void { if (this.shouldRemove(uri)) { this.removeCachedTextDocument(uri)