Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: folding range support #2169

Merged
merged 17 commits into from
Oct 10, 2023
Merged

Conversation

jasonlyu123
Copy link
Member

@jasonlyu123 jasonlyu123 commented Sep 26, 2023

#1704 #1120

This adds the syntactic folding range support instead of the VSCode's default indentation-based and regex-based folding. For embedded languages like Pug and Sass, I added a simplified version of indentation folding. The indentation folding is also a fallback for svelte blocks if there is a parser error.

For users who prefer the speed of the indent-based folding, You can still roll back with the following config.

{
    "[svelte]": {
        "editor.foldingStrategy": "indentation"
    }
}

Implementation

The commits might be messy because I started this nearly a year ago and have done part of this every few months 😆. The process is "the TypeScript result is good enough that's used it for HTML and svelte block as well" to "the TypeScrip result has a lot of weird cases, that's use HTML language service and svelte ast instead".

Now, the TypeScript folding handles everything inside the script tags and any folding ranges that are one-to-one in the generated code, i.e. it's untouched or only being moved around. Component tag and HTML tag are handled by HTML language service. Svelte blocks like each, if and await are extracted from Svelte AST. And folded from the end of the start tag to the start of the end tag. For example,

{#if hi}<!-- start here-->
  {hi}
<!-- end here-->{/if}

VSCode only supports line folding, so it'll start at line 0 and end at line 1, leaving line 2 visible after fold.

{#if hi}...
{/if}

@jasonlyu123 jasonlyu123 marked this pull request as draft September 26, 2023 09:29
@jasonlyu123 jasonlyu123 marked this pull request as ready for review October 3, 2023 07:11
Copy link
Member

@dummdidumm dummdidumm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Damn, impressive work!

General thought: We're using various getText() operations like indexOf, split(/\n\r?/) etc in many places - maybe it's worth having some of these operations move into the document in a more performant manner (for example, keeping the splitted lines cached etc).

@dummdidumm dummdidumm merged commit d637d4e into sveltejs:master Oct 10, 2023
2 checks passed
@jasonlyu123 jasonlyu123 deleted the folding-range branch October 17, 2023 04:47
case 'postcss':
case 'sass':
case 'stylus':
case 'styl':
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants