diff --git a/extensions/typescript-language-features/src/features/bufferSyncSupport.ts b/extensions/typescript-language-features/src/features/bufferSyncSupport.ts index b8db32733a2..c2faca72c50 100644 --- a/extensions/typescript-language-features/src/features/bufferSyncSupport.ts +++ b/extensions/typescript-language-features/src/features/bufferSyncSupport.ts @@ -4,15 +4,14 @@ *--------------------------------------------------------------------------------------------*/ import * as fs from 'fs'; -import { CancellationTokenSource, Disposable, TextDocument, TextDocumentChangeEvent, TextDocumentContentChangeEvent, Uri, workspace, EventEmitter } from 'vscode'; +import { CancellationTokenSource, Disposable, EventEmitter, TextDocument, TextDocumentChangeEvent, TextDocumentContentChangeEvent, Uri, workspace } from 'vscode'; import * as Proto from '../protocol'; import { ITypeScriptServiceClient } from '../typescriptService'; +import API from '../utils/api'; import { Delayer } from '../utils/async'; import { disposeAll } from '../utils/dispose'; import * as languageModeIds from '../utils/languageModeIds'; -import API from '../utils/api'; -import { memoize } from '../utils/memoize'; -import { getTempFile } from '../utils/temp'; +import { ResourceMap } from './resourceMap'; enum BufferKind { TypeScript = 1, @@ -115,84 +114,6 @@ class SyncedBuffer { } } -/** - * Maps of file resources - * - * Attempts to handle correct mapping on both case sensitive and case in-sensitive - * file systems. - */ -class ResourceMap { - private readonly _map = new Map(); - - constructor( - private readonly _normalizePath: (resource: Uri) => string | null - ) { } - - public has(resource: Uri): boolean { - const file = this.toKey(resource); - return !!file && this._map.has(file); - } - - public get(resource: Uri): T | undefined { - const file = this.toKey(resource); - return file ? this._map.get(file) : undefined; - } - - public set(resource: Uri, value: T) { - const file = this.toKey(resource); - if (file) { - this._map.set(file, value); - } - } - - public delete(resource: Uri): void { - const file = this.toKey(resource); - if (file) { - this._map.delete(file); - } - } - - public get values(): Iterable { - return this._map.values(); - } - - public get keys(): Iterable { - return this._map.keys(); - } - - private toKey(resource: Uri): string | null { - const key = this._normalizePath(resource); - if (!key) { - return key; - } - - return this.isCaseInsensitivePath(key) ? key.toLowerCase() : key; - } - - private isCaseInsensitivePath(path: string) { - if (isWindowsPath(path)) { - return true; - } - - return path[0] === '/' && this.onIsCaseInsenitiveFileSystem; - } - - @memoize - private get onIsCaseInsenitiveFileSystem() { - if (process.platform === 'win32') { - return true; - } - - if (process.platform !== 'darwin') { - return false; - } - - const temp = getTempFile('typescript-case-check'); - fs.writeFileSync(temp, ''); - return fs.existsSync(temp.toUpperCase()); - } -} - class SyncedBufferMap extends ResourceMap { public getForPath(filePath: string): SyncedBuffer | undefined { @@ -208,7 +129,6 @@ class SyncedBufferMap extends ResourceMap { } } - export default class BufferSyncSupport { private readonly client: ITypeScriptServiceClient; @@ -445,6 +365,6 @@ export default class BufferSyncSupport { } } -function isWindowsPath(path: string): boolean { +export function isWindowsPath(path: string): boolean { return /^[a-zA-Z]:\\/.test(path); } diff --git a/extensions/typescript-language-features/src/features/resourceMap.ts b/extensions/typescript-language-features/src/features/resourceMap.ts new file mode 100644 index 00000000000..22959a08b89 --- /dev/null +++ b/extensions/typescript-language-features/src/features/resourceMap.ts @@ -0,0 +1,84 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import * as fs from 'fs'; +import { Uri } from 'vscode'; +import { memoize } from '../utils/memoize'; +import { getTempFile } from '../utils/temp'; +import { isWindowsPath } from './bufferSyncSupport'; + +/** + * Maps of file resources + * + * Attempts to handle correct mapping on both case sensitive and case in-sensitive + * file systems. + */ +export class ResourceMap { + private readonly _map = new Map(); + + constructor( + private readonly _normalizePath: (resource: Uri) => string | null + ) { } + + public has(resource: Uri): boolean { + const file = this.toKey(resource); + return !!file && this._map.has(file); + } + + public get(resource: Uri): T | undefined { + const file = this.toKey(resource); + return file ? this._map.get(file) : undefined; + } + + public set(resource: Uri, value: T) { + const file = this.toKey(resource); + if (file) { + this._map.set(file, value); + } + } + + public delete(resource: Uri): void { + const file = this.toKey(resource); + if (file) { + this._map.delete(file); + } + } + + public get values(): Iterable { + return this._map.values(); + } + + public get keys(): Iterable { + return this._map.keys(); + } + + private toKey(resource: Uri): string | null { + const key = this._normalizePath(resource); + if (!key) { + return key; + } + return this.isCaseInsensitivePath(key) ? key.toLowerCase() : key; + } + + private isCaseInsensitivePath(path: string) { + if (isWindowsPath(path)) { + return true; + } + return path[0] === '/' && this.onIsCaseInsenitiveFileSystem; + } + + @memoize + private get onIsCaseInsenitiveFileSystem() { + if (process.platform === 'win32') { + return true; + } + if (process.platform !== 'darwin') { + return false; + } + const temp = getTempFile('typescript-case-check'); + fs.writeFileSync(temp, ''); + return fs.existsSync(temp.toUpperCase()); + } +} diff --git a/extensions/typescript-language-features/src/utils/dependentRegistration.ts b/extensions/typescript-language-features/src/utils/dependentRegistration.ts index 88e0cf00b02..18a0a2d7421 100644 --- a/extensions/typescript-language-features/src/utils/dependentRegistration.ts +++ b/extensions/typescript-language-features/src/utils/dependentRegistration.ts @@ -5,8 +5,8 @@ import * as vscode from 'vscode'; import { ITypeScriptServiceClient } from '../typescriptService'; -import API from '../utils/api'; -import { disposeAll } from '../utils/dispose'; +import API from './api'; +import { disposeAll } from './dispose'; class ConditionalRegistration { private registration: vscode.Disposable | undefined = undefined;