Skip to content

Commit

Permalink
use svelte ast for svelte block folding
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonlyu123 committed Oct 3, 2023
1 parent aa81c16 commit cc97056
Show file tree
Hide file tree
Showing 25 changed files with 403 additions and 88 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { EncodedSourceMap, TraceMap, originalPositionFor } from '@jridgewell/trace-mapping';
import path from 'path';
import { walk } from 'svelte/compiler';
import { TemplateNode } from 'svelte/types/compiler/interfaces';
import { svelte2tsx, IExportedNames, internalHelpers } from 'svelte2tsx';
import ts from 'typescript';
Expand All @@ -19,7 +17,7 @@ import {
} from '../../lib/documents';
import { pathToUrl, urlToPath } from '../../utils';
import { ConsumerDocumentMapper } from './DocumentMapper';
import { SvelteNode } from './svelte-ast-utils';
import { SvelteNode, SvelteNodeWalker, walkSvelteAst } from './svelte-ast-utils';
import {
getScriptKindFromAttributes,
getScriptKindFromFileName,
Expand Down Expand Up @@ -335,19 +333,19 @@ export class SvelteDocumentSnapshot implements DocumentSnapshot {
: this.parent.offsetAt(positionOrOffset);

let foundNode: SvelteNode | null = null;
walk(this.htmlAst as any, {
this.walkSvelteAst({
enter(node) {
// In case the offset is at a point where a node ends and a new one begins,
// the node where the code ends is used. If this introduces problems, introduce
// an affinity parameter to prefer the node where it ends/starts.
if ((node as SvelteNode).start > offset || (node as SvelteNode).end < offset) {
if (node.start > offset || node.end < offset) {
this.skip();
return;
}
const parent = foundNode;
// Spread so the "parent" property isn't added to the original ast,
// causing an infinite loop
foundNode = { ...node } as SvelteNode;
foundNode = { ...node };
if (parent) {
foundNode.parent = parent;
}
Expand All @@ -357,6 +355,14 @@ export class SvelteDocumentSnapshot implements DocumentSnapshot {
return foundNode;
}

walkSvelteAst(walker: SvelteNodeWalker) {
if (!this.htmlAst) {
return;
}

walkSvelteAst(this.htmlAst, walker);
}

getOriginalPosition(pos: Position): Position {
return this.getMapper().getOriginalPosition(pos);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ export class TypeScriptPlugin
return [];
}

const { lang, tsDoc } = await this.getLSAndTSDoc(document);
const { lang, tsDoc } = await this.lsAndTsDocResolver.getLsForSyntheticOperations(document);

if (cancellationToken?.isCancellationRequested) {
return [];
Expand Down Expand Up @@ -366,7 +366,7 @@ export class TypeScriptPlugin
}

async getDefinitions(document: Document, position: Position): Promise<DefinitionLink[]> {
const { lang, tsDoc } = await this.getLSAndTSDoc(document);
const { lang, tsDoc } = await this.lsAndTsDocResolver.getLSAndTSDoc(document);

const defs = lang.getDefinitionAndBoundSpan(
tsDoc.filePath,
Expand Down Expand Up @@ -644,10 +644,6 @@ export class TypeScriptPlugin
return this.foldingRangeProvider.getFoldingRanges(document);
}

private async getLSAndTSDoc(document: Document) {
return this.lsAndTsDocResolver.getLSAndTSDoc(document);
}

/**
* @internal Public for tests only
*/
Expand Down
Loading

0 comments on commit cc97056

Please sign in to comment.