问题
I am working on a language extension for SAS for VScode. I previously worked on the SAS language extension for Atom (https://github.com/akanosora/language-sas) as well as Vim (part of the default Vim packages: https://github.com/vim/vim/blob/master/runtime/indent/sas.vim).
I am not very satisfied with the auto-indentation implementation in Atom and it seems that VScode provides more or less the same mechanism for auto-indentation.
The proper indentation for SAS code is quite tricky as the closing of a block is not always mandatory. A block in SAS typically starts with data
or proc
and ends with run
or quit
, and you can skip run
to close it. For example, the following codes are both okay in SAS:
data female;
set total;
where gender = 0;
run;
data male;
set total;
where gender = 1;
run;
or
data female;
set total;
where gender = 0;
data male;
set total;
where gender = 1;
run;
So a proper auto-indentation for SAS requires more complicated rules than increaseIndentPattern
and decreaseIndentPattern
as they do not always pair with each other. I was able to implement that in Vim by comparing the closest run
and data
above the current line. If the run
is more close to the current data
line than the previous data
line, then no indent is needed. Otherwise, indent the current data
line. I want to know how feasible it is to implement that in VScode (maybe not relying on the indentationRules
setting but use vscode.languages.*
API?) I need some directions.
回答1:
For more advanced indentation, you could look into using a formatter alongside the regular expression based indentation rules. To add a custom formatter, your extension should implement DocumentRangeFormattingEditProvider and then register the with VS Code by calling registerDocumentRangeFormattingEditProvider
Formatters must normally be invoked by the user. However users can also enable formatting as they type by setting "editor.formatOnType": true
. For this case, your extension should also implement OnTypeFormattingEditProvider and register the on-type formatter by calling registerOnTypeFormattingEditProvider
来源:https://stackoverflow.com/questions/57080997/how-to-implement-complicated-auto-indentation-in-vscode