-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix #7269: complete support vscode.workspace.fs API
Signed-off-by: Anton Kosyakov <[email protected]>
- Loading branch information
Showing
31 changed files
with
3,582 additions
and
766 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
/** | ||
* @returns New array with all falsy values removed. The original array IS NOT modified. | ||
*/ | ||
export function coalesce<T>(array: ReadonlyArray<T | undefined | null>): T[] { | ||
return <T[]>array.filter(e => !!e); | ||
} | ||
|
||
/** | ||
* @returns True if the provided object is an array and has at least one element. | ||
*/ | ||
export function isNonEmptyArray<T>(obj: T[] | undefined | null): obj is T[]; | ||
export function isNonEmptyArray<T>(obj: readonly T[] | undefined | null): obj is readonly T[]; | ||
export function isNonEmptyArray<T>(obj: T[] | readonly T[] | undefined | null): obj is T[] | readonly T[] { | ||
return Array.isArray(obj) && obj.length > 0; | ||
} |
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,87 @@ | ||
const hasBuffer = (typeof Buffer !== 'undefined'); | ||
const hasTextEncoder = (typeof TextEncoder !== 'undefined'); | ||
|
||
export class VSBuffer { | ||
|
||
static alloc(byteLength: number): VSBuffer { | ||
if (hasBuffer) { | ||
return new VSBuffer(Buffer.allocUnsafe(byteLength)); | ||
} else { | ||
return new VSBuffer(new Uint8Array(byteLength)); | ||
} | ||
} | ||
|
||
static wrap(actual: Uint8Array): VSBuffer { | ||
if (hasBuffer && !(Buffer.isBuffer(actual))) { | ||
// https://nodejs.org/dist/latest-v10.x/docs/api/buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length | ||
// Create a zero-copy Buffer wrapper around the ArrayBuffer pointed to by the Uint8Array | ||
actual = Buffer.from(actual.buffer, actual.byteOffset, actual.byteLength); | ||
} | ||
return new VSBuffer(actual); | ||
} | ||
|
||
static fromString(source: string): VSBuffer { | ||
if (hasBuffer) { | ||
return new VSBuffer(Buffer.from(source)); | ||
} else if (hasTextEncoder) { | ||
if (!textEncoder) { | ||
textEncoder = new TextEncoder(); | ||
} | ||
return new VSBuffer(textEncoder.encode(source)); | ||
} else { | ||
return new VSBuffer(strings.encodeUTF8(source)); | ||
} | ||
} | ||
|
||
readonly buffer: Uint8Array; | ||
readonly byteLength: number; | ||
|
||
private constructor(buffer: Uint8Array) { | ||
this.buffer = buffer; | ||
this.byteLength = this.buffer.byteLength; | ||
} | ||
|
||
toString(): string { | ||
if (hasBuffer) { | ||
return this.buffer.toString(); | ||
} else if (hasTextDecoder) { | ||
if (!textDecoder) { | ||
textDecoder = new TextDecoder(); | ||
} | ||
return textDecoder.decode(this.buffer); | ||
} else { | ||
return strings.decodeUTF8(this.buffer); | ||
} | ||
} | ||
|
||
slice(start?: number, end?: number): VSBuffer { | ||
// IMPORTANT: use subarray instead of slice because TypedArray#slice | ||
// creates shallow copy and NodeBuffer#slice doesn't. The use of subarray | ||
// ensures the same, performant, behaviour. | ||
return new VSBuffer(this.buffer.subarray(start, end)); | ||
} | ||
|
||
} | ||
|
||
export interface VSBufferReadable { | ||
|
||
read(): VSBuffer | null; | ||
|
||
} | ||
|
||
export function bufferToReadable(buffer: VSBuffer): VSBufferReadable { | ||
let consumed = false; | ||
|
||
return { | ||
read: () => { | ||
if (consumed) { | ||
return null; | ||
} | ||
|
||
consumed = true; | ||
|
||
return buffer; | ||
} | ||
}; | ||
} | ||
|
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,72 @@ | ||
/******************************************************************************** | ||
* Copyright (C) 2020 TypeFox and others. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License v. 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0. | ||
* | ||
* This Source Code may also be made available under the following Secondary | ||
* Licenses when the conditions for such availability set forth in the Eclipse | ||
* Public License v. 2.0 are satisfied: GNU General Public License, version 2 | ||
* with the GNU Classpath Exception which is available at | ||
* https://www.gnu.org/software/classpath/license.html. | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 | ||
********************************************************************************/ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { toUint8 } from './uint'; | ||
|
||
/** | ||
* A fast character classifier that uses a compact array for ASCII values. | ||
*/ | ||
export class CharacterClassifier<T extends number> { | ||
/** | ||
* Maintain a compact (fully initialized ASCII map for quickly classifying ASCII characters - used more often in code). | ||
*/ | ||
protected _asciiMap: Uint8Array; | ||
|
||
/** | ||
* The entire map (sparse array). | ||
*/ | ||
protected _map: Map<number, number>; | ||
|
||
protected _defaultValue: number; | ||
|
||
constructor(_defaultValue: T) { | ||
const defaultValue = toUint8(_defaultValue); | ||
|
||
this._defaultValue = defaultValue; | ||
this._asciiMap = CharacterClassifier._createAsciiMap(defaultValue); | ||
this._map = new Map<number, number>(); | ||
} | ||
|
||
private static _createAsciiMap(defaultValue: number): Uint8Array { | ||
const asciiMap: Uint8Array = new Uint8Array(256); | ||
for (let i = 0; i < 256; i++) { | ||
asciiMap[i] = defaultValue; | ||
} | ||
return asciiMap; | ||
} | ||
|
||
public set(charCode: number, _value: T): void { | ||
const value = toUint8(_value); | ||
|
||
if (charCode >= 0 && charCode < 256) { | ||
this._asciiMap[charCode] = value; | ||
} else { | ||
this._map.set(charCode, value); | ||
} | ||
} | ||
|
||
public get(charCode: number): T { | ||
if (charCode >= 0 && charCode < 256) { | ||
return <T>this._asciiMap[charCode]; | ||
} else { | ||
return <T>(this._map.get(charCode) || this._defaultValue); | ||
} | ||
} | ||
} |
Oops, something went wrong.