summaryrefslogtreecommitdiff
path: root/tools/vscode/packages/server/src/utils.ts
diff options
context:
space:
mode:
authorGravatar Nate Moore <natemoo-re@users.noreply.github.com> 2021-04-30 16:33:35 -0500
committerGravatar GitHub <noreply@github.com> 2021-04-30 16:33:35 -0500
commit4df1347156cf2632ea2f3475d3a5f8f08d197cc3 (patch)
tree9d50de89dfe62827c32a8a4046120af4ab61dc0c /tools/vscode/packages/server/src/utils.ts
parent1d498facc8f78a3ffbfecd05cc6ecd45e8a4a1ae (diff)
downloadastro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.tar.gz
astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.tar.zst
astro-4df1347156cf2632ea2f3475d3a5f8f08d197cc3.zip
Migrate to `yarn` monorepo (#157)
* chore: use monorepo * chore: scaffold astro-scripts * chore: move tests inside packages/astro * chore: refactor tests, add scripts * chore: move parser to own module * chore: move runtime to packages/astro * fix: move parser to own package * test: fix prettier-plugin-astro tests * fix: tests * chore: update package-lock * chore: add changesets * fix: cleanup examples * fix: starter example * chore: update changeset config * chore: update changeset config * chore: setup changeset release workflow * chore: bump lockfiles * chore: prism => astro-prism * fix: tsc --emitDeclarationOnly * chore: final cleanup, switch to yarn * chore: add lerna * chore: update workflows to yarn * chore: update workflows * chore: remove lint workflow * chore: add astro-dev script * chore: add symlinked README
Diffstat (limited to 'tools/vscode/packages/server/src/utils.ts')
-rw-r--r--tools/vscode/packages/server/src/utils.ts98
1 files changed, 98 insertions, 0 deletions
diff --git a/tools/vscode/packages/server/src/utils.ts b/tools/vscode/packages/server/src/utils.ts
new file mode 100644
index 000000000..c764aae13
--- /dev/null
+++ b/tools/vscode/packages/server/src/utils.ts
@@ -0,0 +1,98 @@
+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 {
+ return URI.parse(uri).toString();
+}
+
+/** Turns a URL into a normalized FS Path */
+export function urlToPath(stringUrl: string): string | null {
+ const url = URI.parse(stringUrl);
+ if (url.scheme !== 'file') {
+ return null;
+ }
+ return url.fsPath.replace(/\\/g, '/');
+}
+
+/** Converts a path to a URL */
+export function pathToUrl(path: string) {
+ return URI.file(path).toString();
+}
+
+
+/**
+*
+* 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.
+*/
+export function isPossibleComponent(node: Node): boolean {
+ return !!node.tag?.[0].match(/[A-Z]/);
+}
+
+/**
+*
+* 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.
+*/
+export function isPossibleClientComponent(node: Node): boolean {
+ return isPossibleComponent(node) && (node.tag?.indexOf(':') ?? -1) > -1;
+}
+
+/** Flattens an array */
+export function flatten<T>(arr: T[][]): T[] {
+ return arr.reduce((all, item) => [...all, ...item], []);
+}
+
+/** Clamps a number between min and max */
+export function clamp(num: number, min: number, max: number): number {
+ return Math.max(min, Math.min(max, num));
+}
+
+/** Checks if a position is inside range */
+export function isInRange(positionToTest: Position, range: Range): boolean {
+ return (
+ isBeforeOrEqualToPosition(range.end, positionToTest) &&
+ isBeforeOrEqualToPosition(positionToTest, range.start)
+ );
+}
+
+/** */
+export function isBeforeOrEqualToPosition(position: Position, positionToTest: Position): boolean {
+ return (
+ positionToTest.line < position.line ||
+ (positionToTest.line === position.line && positionToTest.character <= position.character)
+ );
+}
+
+/**
+ * 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
+ */
+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;
+
+ return (arg: T) => {
+ if (shouldCancelPrevious(arg, prevArg)) {
+ clearTimeout(timeout);
+ }
+
+ prevArg = arg;
+ timeout = setTimeout(() => {
+ fn(arg);
+ prevArg = undefined;
+ }, miliseconds);
+ };
+}