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

format new text in code action and completion #1598

Merged
merged 8 commits into from
Aug 30, 2022

Conversation

jasonlyu123
Copy link
Member

@jasonlyu123 jasonlyu123 commented Aug 24, 2022

For #1579. #854 Adding the default format config from typescript and overriding per file it with resolved prettier config. There're some I would like to discuss:

  1. Should we read the config from vscode ts/js config? I didn't want to read that because these configs won't be used for formatting. I am afraid it would cause some confusion. Or should we just make it separate and don't use the prettier config?

  2. How many of the configs should we detect from the file? The line ending makes sense since by default git on the window would format the line ending. So windows people would want to develop in CRLF and have git convert to LF. What about semi? Seems like TypeScript would detect it if the config is set to ignore. Maybe we should only force it to add or remove in organize-import?

https://github.com/microsoft/TypeScript/blob/394f51aeed80788dca72c6f6a90d1d27886b6972/src/services/utilities.ts#L3411

  1. If we go with the prettier config route. Should we use a different default config with TypeScript? Prettier use 2 spaces by default. But VSCode uses 4 by default.

@dummdidumm what do you think?

...(await this.configManager.getFormatCodeSettingsForFile(document)),

// handle it on our own
baseIndentSize: undefined
Copy link
Member Author

@jasonlyu123 jasonlyu123 Aug 24, 2022

Choose a reason for hiding this comment

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

baseIndent has some trouble here. Because we don't have indents for import in the svelte2tsx. it would make it always have organize-import. Think it would make more sense to it on our own.

@dummdidumm
Copy link
Member

All these different tools wanting to have a say in code formatting, it's messy 😄 I think your approach of lettering prettier taking preference makes sense since that preserves existing behavior. I didn't fully understand what do you mean by "I don't want to use the VS Code TS/JS settings" - what would be the problem exactly? If someone has Prettier turned off, these settings would have impact since they are not obsolete after formatting (if that's what you meant).

@jasonlyu123
Copy link
Member Author

Did you mean we'll always use the ts/js format config? Or do we provide a config or a condition for using the ts/js config?

@dummdidumm
Copy link
Member

What I imagined was:

  1. Try to load VS Code ts/js format config
  2. Overwrite specific parts with what we know from the Prettier config (the semi and line breaks)
  3. Fall back to defaults (the ones you listed in that PR already)

}

const prettierConfig = this.getMergedPrettierConfig(
await importPrettier(filePath).resolveConfig(filePath, {
Copy link
Member Author

Choose a reason for hiding this comment

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

I wonder if we could disable the "using prettier 2.x.x from path" logging here. It's kind of disturbing. Als it runs on completion which triggers quite often it might hurt performance.

Copy link
Member

Choose a reason for hiding this comment

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

Yeah that makes sense

@jasonlyu123 jasonlyu123 marked this pull request as ready for review August 29, 2022 01:44
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.

Looks good! I agree that we should remove the logging in this case - maybe we should remove it altogether, I can't remember when this was useful last time (it was in the early days, but not anymore). Thoughts on that?

Another thought: We query prettier on every autocompletion now, I wonder if this has any performance impact. I think we should be fine since the dynamic import is only slow the first time and cached later on, same for resolving.

@jasonlyu123
Copy link
Member Author

Do you mean remove all version logging or only prettier? Maybe we could add a logger.debug and config to enable it. And we can remove all the version logging.

About import prettier. I benchmark it on my machine. Logging the first time, then 1000 times with log and 1000 times without log. The cold start run is about 110ms. 1000x without logging in 150-170ms. 1000x with log is about 10% slower. So yeah, it's only slow on the first one. it's fast enough for later calls.

@dummdidumm
Copy link
Member

I like the Logger.debug + config idea. Name could be svelte.language-server.debug = true/false which is a generic enough name that we could add other things to it that are present in debug mode only.

@@ -13,4 +19,10 @@ export class Logger {
static error(...args: any) {
console.error(...args);
}

static debug(...args: any) {
if (!Logger.logErrorsOnly && this.logDebug) {
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
if (!Logger.logErrorsOnly && this.logDebug) {
if (!Logger.logErrorsOnly && Logger.logDebug) {

@@ -180,6 +180,7 @@ async function createLanguageService(
let projectVersion = 0;

const host: ts.LanguageServiceHost = {
log: (message) => Logger.debug(`[ts] ${message}`),
Copy link
Member

Choose a reason for hiding this comment

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

Oh interesting 😄

@dummdidumm dummdidumm merged commit 7e496d3 into sveltejs:master Aug 30, 2022
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.

None yet

2 participants