-
Notifications
You must be signed in to change notification settings - Fork 27.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Clean up structure of markdown extension (#161148)
- Move things related to the client under `client` - Remove extra abstractions that are no longer used - Add MdLanguageClient type
- Loading branch information
Showing
17 changed files
with
183 additions
and
226 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
extensions/markdown-language-features/src/client/fileWatchingManager.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import { disposeAll } from 'vscode-markdown-languageservice/out/util/dispose'; | ||
import { ResourceMap } from 'vscode-markdown-languageservice/out/util/resourceMap'; | ||
import { Utils } from 'vscode-uri'; | ||
import { IDisposable } from '../util/dispose'; | ||
import { Schemes } from '../util/schemes'; | ||
|
||
type DirWatcherEntry = { | ||
readonly uri: vscode.Uri; | ||
readonly listeners: IDisposable[]; | ||
}; | ||
|
||
|
||
export class FileWatcherManager { | ||
|
||
private readonly fileWatchers = new Map<number, { | ||
readonly watcher: vscode.FileSystemWatcher; | ||
readonly dirWatchers: DirWatcherEntry[]; | ||
}>(); | ||
|
||
private readonly dirWatchers = new ResourceMap<{ | ||
readonly watcher: vscode.FileSystemWatcher; | ||
refCount: number; | ||
}>(); | ||
|
||
create(id: number, uri: vscode.Uri, watchParentDirs: boolean, listeners: { create?: () => void; change?: () => void; delete?: () => void }): void { | ||
const watcher = vscode.workspace.createFileSystemWatcher(new vscode.RelativePattern(uri, '*'), !listeners.create, !listeners.change, !listeners.delete); | ||
const parentDirWatchers: DirWatcherEntry[] = []; | ||
this.fileWatchers.set(id, { watcher, dirWatchers: parentDirWatchers }); | ||
|
||
if (listeners.create) { watcher.onDidCreate(listeners.create); } | ||
if (listeners.change) { watcher.onDidChange(listeners.change); } | ||
if (listeners.delete) { watcher.onDidDelete(listeners.delete); } | ||
|
||
if (watchParentDirs && uri.scheme !== Schemes.untitled) { | ||
// We need to watch the parent directories too for when these are deleted / created | ||
for (let dirUri = Utils.dirname(uri); dirUri.path.length > 1; dirUri = Utils.dirname(dirUri)) { | ||
const dirWatcher: DirWatcherEntry = { uri: dirUri, listeners: [] }; | ||
|
||
let parentDirWatcher = this.dirWatchers.get(dirUri); | ||
if (!parentDirWatcher) { | ||
const glob = new vscode.RelativePattern(Utils.dirname(dirUri), Utils.basename(dirUri)); | ||
const parentWatcher = vscode.workspace.createFileSystemWatcher(glob, !listeners.create, true, !listeners.delete); | ||
parentDirWatcher = { refCount: 0, watcher: parentWatcher }; | ||
this.dirWatchers.set(dirUri, parentDirWatcher); | ||
} | ||
parentDirWatcher.refCount++; | ||
|
||
if (listeners.create) { | ||
dirWatcher.listeners.push(parentDirWatcher.watcher.onDidCreate(async () => { | ||
// Just because the parent dir was created doesn't mean our file was created | ||
try { | ||
const stat = await vscode.workspace.fs.stat(uri); | ||
if (stat.type === vscode.FileType.File) { | ||
listeners.create!(); | ||
} | ||
} catch { | ||
// Noop | ||
} | ||
})); | ||
} | ||
|
||
if (listeners.delete) { | ||
// When the parent dir is deleted, consider our file deleted too | ||
// TODO: this fires if the file previously did not exist and then the parent is deleted | ||
dirWatcher.listeners.push(parentDirWatcher.watcher.onDidDelete(listeners.delete)); | ||
} | ||
|
||
parentDirWatchers.push(dirWatcher); | ||
} | ||
} | ||
} | ||
|
||
delete(id: number): void { | ||
const entry = this.fileWatchers.get(id); | ||
if (entry) { | ||
for (const dirWatcher of entry.dirWatchers) { | ||
disposeAll(dirWatcher.listeners); | ||
|
||
const dirWatcherEntry = this.dirWatchers.get(dirWatcher.uri); | ||
if (dirWatcherEntry) { | ||
if (--dirWatcherEntry.refCount <= 0) { | ||
dirWatcherEntry.watcher.dispose(); | ||
this.dirWatchers.delete(dirWatcher.uri); | ||
} | ||
} | ||
} | ||
|
||
entry.watcher.dispose(); | ||
} | ||
|
||
this.fileWatchers.delete(id); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.