Skip to content

Commit

Permalink
✨ Exclude patterns using micromatch
Browse files Browse the repository at this point in the history
  • Loading branch information
misode committed Dec 29, 2024
1 parent 347f69e commit 715085f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 6 deletions.
36 changes: 34 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
},
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/service/CacheService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/service/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ export const VanillaConfig: Config = {
env: {
dataSource: 'GitHub',
dependencies: ['@vanilla-datapack', '@vanilla-resourcepack', '@vanilla-mcdoc'],
exclude: [],
exclude: ['.*/**'],
customResources: {},
feature: {
codeActions: true,
Expand Down
31 changes: 28 additions & 3 deletions packages/core/src/service/Project.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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<string>()
Expand Down Expand Up @@ -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 })
Expand Down Expand Up @@ -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)
Expand All @@ -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(
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 715085f

Please sign in to comment.