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

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

问题描述

我正在为 SAS for VScode 开发语言扩展.我之前从事过 Atom 的 SAS 语言扩展 (https://github.com/akanosora/language-sas)以及 Vim(默认 Vim 包的一部分:https://github.com/vim/vim/blob/master/runtime/indent/sas.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 结尾,您可以跳过 <代码>运行关闭它.例如,以下代码在 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; 

因此,SAS 的正确自动缩进需要比 increaseIndentPatterndecreaseIndentPattern 更复杂的规则,因为它们并不总是相互配对.我可以通过比较当前行上方最接近的 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.

考虑一下,例如:

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

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

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