如何在VScode中实现复杂的自动缩进 [英] How to implement complicated auto-indentation in VScode

查看:509
本文介绍了如何在VScode中实现复杂的自动缩进的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为VScode的SAS开发语言扩展.我以前曾为Atom开发SAS语言扩展( https://github.com/akanosora/language-sas )以及Vim(默认Vim软件包的一部分:

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).

我对Atom中的自动缩进实现不太满意,似乎VScode或多或少提供了相同的自动缩进机制.

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.

正确地插入SAS代码非常棘手,因为关闭块并不总是强制性的. SAS中的块通常以dataproc开头,以runquit结尾,您可以跳过run将其关闭.例如,以下代码在SAS中都可以使用:

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; 

data female; 
    set total;
    where gender = 0;

data male; 
    set total;
    where gender = 1;
run; 

因此,与increaseIndentPatterndecreaseIndentPattern相比,正确的SAS自动压痕需要更复杂的规则,因为它们并不总是相互配对.通过比较当前行上方最接近的rundata,我可以在Vim中实现该功能.如果run比上一个data行更靠近当前data行,则不需要缩进.否则,缩进当前的data行.我想知道在VScode中实现它的可行性(也许不依赖indentationRules设置而是使用vscode.languages.* API?)我需要一些指导.

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.

推荐答案

您可以将dataproc关键字本身添加到"decreaseIndentPattern"规则中.这样,这些关键字既可以使当前行缩进,又可以在下一行开始新的缩进块.

You can add the data and proc keywords themselves to the "decreaseIndentPattern" rule. This way, these keywords will serve for both un-indenting the current line, and starting a new indentation block on the next line.

请考虑以下内容,例如:

Consider this, for example:

"indentationRules": {
    "increaseIndentPattern": "^\\s*(proc|data)\\s+.*;\\s*$",
    "decreaseIndentPattern": "^\\s*(run|((proc|data)\\s+.*))\\s*;"
}

这篇关于如何在VScode中实现复杂的自动缩进的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆