How to implement complicated auto-indentation in VScode

跟風遠走 提交于 2019-12-14 03:53:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!