You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
What started as a simple fix became a refactor that was needed anyway, so the sooner, the better.
The refactor was required due to a limitation we had in our WorkspaceServer. Every time we save a Document in our workspace, we store it together with an information called language_hint. This is used to tell the workspace the kind of language that belongs to a file.
However, there are information that we aren't able to compute from the file extension. For example, a .js file can be a script or a module, and this info, still today, can't be correctly computed. Plus, we couldn't tell the formatter that trailing commas are allowed, because this option is saved in the parsing options. These parsing options aren't available when computing the formatting options.
This refactor removes the Language enum and replaces it with a new enum called DocumentFileSource. The DocumentFileSource exposes the same functions that Language does, but it holds the information of a file source: e.g. JsFileSource, JsonFileSource, etc.
Initially, DocumentFileSource is inferred from the file extension so that logic isn't changed. Although we now pass this type around, we are also able to mutate it during the parsing phase.
For example, check the json.rs file in the parse() function. There, we mutate the JsonFileSource and change the newly created allow_trailing_comma.
In order to reduce memory usage, I created a new field called file_sources, which is an IndexSet, where we save all the known file sources that we find. Inside the Document struct we save the index of the newly created file source.
The idea is the following: if we analyze 100 .js files, it's safe to assume that all of them have the same JsFileSource, so we have only one copy index file_source, and we always retrieve the same when we need it.
Test Plan
As you might have noticed, now the JSOC files are correctly formatted :)
Still, I have a small question. Do you think it is reasonable to provide a dedicated trailing_commas_control option for the json formatter as well? So that we can use a forgiving option in the parser (allow trailing commas) but a stricter option in the formatter (omit trailing commas).
This is most useful when formatting tsconfig.json files, because although it can include trailing commas, 1) it has the extension .json so some other tools may see it as a standard json file, 2) VSCode issues warnings for jsonc files with trailing commas unless the user manually override the settings, 3) there has been a heated debate about this on the Prettier side: prettier/prettier#15956, Prettier first treated tsconfig.json as a jsonc file in v3.2.3 and would add trailing commas to the file, and later in v3.2.5 it reverted the change.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #1659
Closes #1662
What started as a simple fix became a refactor that was needed anyway, so the sooner, the better.
The refactor was required due to a limitation we had in our
WorkspaceServer
. Every time we save aDocument
in our workspace, we store it together with an information calledlanguage_hint
. This is used to tell the workspace the kind of language that belongs to a file.However, there are information that we aren't able to compute from the file extension. For example, a
.js
file can be a script or a module, and this info, still today, can't be correctly computed. Plus, we couldn't tell the formatter that trailing commas are allowed, because this option is saved in the parsing options. These parsing options aren't available when computing the formatting options.This refactor removes the
Language
enum and replaces it with a new enum calledDocumentFileSource
. TheDocumentFileSource
exposes the same functions thatLanguage
does, but it holds the information of a file source: e.g.JsFileSource
,JsonFileSource
, etc.Initially,
DocumentFileSource
is inferred from the file extension so that logic isn't changed. Although we now pass this type around, we are also able to mutate it during the parsing phase.For example, check the
json.rs
file in theparse()
function. There, we mutate theJsonFileSource
and change the newly createdallow_trailing_comma
.In order to reduce memory usage, I created a new field called
file_sources
, which is anIndexSet
, where we save all the known file sources that we find. Inside theDocument
struct we save the index of the newly created file source.The idea is the following: if we analyze 100
.js
files, it's safe to assume that all of them have the sameJsFileSource
, so we have only one copy indexfile_source
, and we always retrieve the same when we need it.Test Plan
As you might have noticed, now the JSOC files are correctly formatted :)