2021-04-23 17:53:01 -05:00
|
|
|
import { URI } from 'vscode-uri';
|
|
|
|
import { Position, Range } from 'vscode-languageserver';
|
|
|
|
import { Node } from 'vscode-html-languageservice';
|
|
|
|
|
|
|
|
/** Normalizes a document URI */
|
|
|
|
export function normalizeUri(uri: string): string {
|
2021-05-03 12:26:10 -06:00
|
|
|
return URI.parse(uri).toString();
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Turns a URL into a normalized FS Path */
|
|
|
|
export function urlToPath(stringUrl: string): string | null {
|
2021-05-03 12:26:10 -06:00
|
|
|
const url = URI.parse(stringUrl);
|
|
|
|
if (url.scheme !== 'file') {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return url.fsPath.replace(/\\/g, '/');
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Converts a path to a URL */
|
|
|
|
export function pathToUrl(path: string) {
|
2021-05-03 12:26:10 -06:00
|
|
|
return URI.file(path).toString();
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-03 12:26:10 -06:00
|
|
|
*
|
|
|
|
* The language service is case insensitive, and would provide
|
|
|
|
* hover info for Svelte components like `Option` which have
|
|
|
|
* the same name like a html tag.
|
|
|
|
*/
|
2021-04-23 17:53:01 -05:00
|
|
|
export function isPossibleComponent(node: Node): boolean {
|
2021-05-03 12:26:10 -06:00
|
|
|
return !!node.tag?.[0].match(/[A-Z]/);
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-05-03 12:26:10 -06:00
|
|
|
*
|
|
|
|
* The language service is case insensitive, and would provide
|
|
|
|
* hover info for Svelte components like `Option` which have
|
|
|
|
* the same name like a html tag.
|
|
|
|
*/
|
2021-04-23 17:53:01 -05:00
|
|
|
export function isPossibleClientComponent(node: Node): boolean {
|
2021-05-03 12:26:10 -06:00
|
|
|
return isPossibleComponent(node) && (node.tag?.indexOf(':') ?? -1) > -1;
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Flattens an array */
|
|
|
|
export function flatten<T>(arr: T[][]): T[] {
|
2021-05-03 12:26:10 -06:00
|
|
|
return arr.reduce((all, item) => [...all, ...item], []);
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Clamps a number between min and max */
|
|
|
|
export function clamp(num: number, min: number, max: number): number {
|
2021-05-03 12:26:10 -06:00
|
|
|
return Math.max(min, Math.min(max, num));
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
2021-05-20 13:02:46 -04:00
|
|
|
export function isNotNullOrUndefined<T>(val: T | undefined | null): val is T {
|
|
|
|
return val !== undefined && val !== null;
|
|
|
|
}
|
|
|
|
|
2021-04-23 17:53:01 -05:00
|
|
|
/** Checks if a position is inside range */
|
|
|
|
export function isInRange(positionToTest: Position, range: Range): boolean {
|
2021-05-03 12:26:10 -06:00
|
|
|
return isBeforeOrEqualToPosition(range.end, positionToTest) && isBeforeOrEqualToPosition(positionToTest, range.start);
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/** */
|
|
|
|
export function isBeforeOrEqualToPosition(position: Position, positionToTest: Position): boolean {
|
2021-05-03 12:26:10 -06:00
|
|
|
return positionToTest.line < position.line || (positionToTest.line === position.line && positionToTest.character <= position.character);
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Debounces a function but cancels previous invocation only if
|
|
|
|
* a second function determines it should.
|
|
|
|
*
|
|
|
|
* @param fn The function with it's argument
|
|
|
|
* @param determineIfSame The function which determines if the previous invocation should be canceld or not
|
|
|
|
* @param miliseconds Number of miliseconds to debounce
|
|
|
|
*/
|
2021-05-03 12:26:10 -06:00
|
|
|
export function debounceSameArg<T>(fn: (arg: T) => void, shouldCancelPrevious: (newArg: T, prevArg?: T) => boolean, miliseconds: number): (arg: T) => void {
|
|
|
|
let timeout: any;
|
|
|
|
let prevArg: T | undefined;
|
2021-04-23 17:53:01 -05:00
|
|
|
|
2021-05-03 12:26:10 -06:00
|
|
|
return (arg: T) => {
|
|
|
|
if (shouldCancelPrevious(arg, prevArg)) {
|
|
|
|
clearTimeout(timeout);
|
|
|
|
}
|
2021-04-23 17:53:01 -05:00
|
|
|
|
2021-05-03 12:26:10 -06:00
|
|
|
prevArg = arg;
|
|
|
|
timeout = setTimeout(() => {
|
|
|
|
fn(arg);
|
|
|
|
prevArg = undefined;
|
|
|
|
}, miliseconds);
|
|
|
|
};
|
2021-04-23 17:53:01 -05:00
|
|
|
}
|