-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Correct enum indentation in vimscript #16293
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Don't forget to update Then, test it locally as follows:
|
I would also consider adding a more complicated test, e.g.: " START_INDENT
enum Digits
INVALID(v:numbermax), # The null value.
ZERO(0 * v:numbermin), ONE(2 - 1),
TWO(1 + 1), THREE(9 / 3), FOUR(1 * 4),
FIVE(1 + 2 + 2), SIX(36 / 3 / 2), SEVEN(7), EIGHT(2 * 2 * 2),
NINE(3 + 3 + 3)
const value: number
def new(this.value)
enddef
endenum
" END_INDENT |
> I would also consider adding a more complicated test, e.g.: > > ```viml > " START_INDENT > enum Digits > INVALID(v:numbermax), # The null value. > ZERO(0 * v:numbermin), ONE(2 - 1), > TWO(1 + 1), THREE(9 / 3), FOUR(1 * 4), > FIVE(1 + 2 + 2), SIX(36 / 3 / 2), SEVEN(7), EIGHT(2 * 2 * 2), > NINE(3 + 3 + 3) > const value: number > def new(this.value) > enddef > endenum > " END_INDENT > ```
The proposed changes work for reformatting the whole |
That's indeed a problem, I'll take some time finding a better solution. |
I believe this is a simple solution to the issue. However, since it is limited to handle enum blocks and partially overlaps with the logic for indenting bracket blocks, further refactoring may be needed in the future to improve the code. |
runtime/autoload/dist/vimindent.vim
Outdated
var enum_pos = search('^\C\s*enum\>\s', 'bnW') | ||
var endenum_pos = search('^\C\s*endenum\>\s', 'bnW') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var enum_pos = search('^\C\s*enum\>\s', 'bnW') | |
var endenum_pos = search('^\C\s*endenum\>\s', 'bnW') | |
var enum_pos = search('^\C\s*\%(export\s\)\=\s*enum\s\+\S\+', 'bnW') | |
var endenum_pos = search('^\C\s*endenum\>', 'bnW') |
The flag combination for search()
can be tricky; the above
code seems to follow in PrevCodeLine()
's footsteps. If the
cursor is inside EnumBlock
, shouldn't it seek forward for
endenum
and backward for enum
? Will it work when there is
more than one enum
definition in the same file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the export enum
case I think we should also map 'enum' with 'export' modifier. It is previously not covered.
--- a/runtime/autoload/dist/vimindent.vim
+++ b/runtime/autoload/dist/vimindent.vim
@@ -172,6 +172,7 @@ const MODIFIERS: dict<string> = {
def: ['export', 'static'],
class: ['export', 'abstract', 'export abstract'],
interface: ['export'],
+ enum: ['export'],
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the
cursor is inside EnumBlock, shouldn't it seek forward for
endenum and backward for enum?
No. This logic may fail. Consider a case
enum Color
Red,
Green,
Blue
endenom # <-- Missed
var foo: number = 1 # <-- This line may fail
enum Subject # <-- Missed
Math,
Physics
endenum
In this case, If we seek for enum
backward and endenum
forward from the marked line, we might miss the endenum
right above and the enum
right below. As a result, the function could incorrectly conclude that the marked line is inside an EnumBlock.
So the simple logic that I came up with is to both seek above. If we find enum
first, then it must be inside an EnumBlock, else if we find endenum
first, it should be outside an enum block.
This works unless there's an EnumBlock inside an EnumBlock, as shown below. In this case we might need to handle it using a stack (like what we did for BracketBlocks), but I think there's no(or rare) such syntax on EnumBlocks so far.
enum Color
enum Subject
Math,
Physics
endenum
Red,
Green
endenum
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just noticed that I did make a sign mistake in the comparison expression.
I thought the return value when searching backward was relative to the current position so I wrote <
sign. (However it just worked, unexpectedly... Bruh…)
Corrected. Thank you again for making me rethink.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair enough. It escaped my notice that the cursor is only
moved once, hence the need to search in one direction.
There is still some room for improvement.
We can do better by avoiding the second lookup as soon as it
is known that there is no enum
above. It is also worth the
effort to verify that the found enum
and/or endenum
line is
not the current line (remembering about NonCommentedMatch
's
timeouts); otherwise, to reject this line for the related
indentation. See #16310.
The latest patch version correctly aligns It would be nice if some future patch tried a bit harder at vim9script
enum RGB
RED(
0xff0000,
0),
GREEN(
0x008000,
0),
BLUE(
0x0000ff,
0)
const value: number
var other: any
def new(value: number, other: any)
this.value = value
this.other = other
enddef
endenum |
Co-authored-by: Aliaksei Budavei <32549825+zzzyxwvut@users.noreply.github.com>
Co-authored-by: Aliaksei Budavei <32549825+zzzyxwvut@users.noreply.github.com>
…ks/Python.framework/Versions/3.13/lib: HOME=/Users/jimzhou HOMEBREW_CELLAR=/opt/homebrew/Cellar HOMEBREW_PREFIX=/opt/homebrew HOMEBREW_REPOSITORY=/opt/homebrew INFOPATH=/opt/homebrew/share/info: LANG=en_US.UTF-8 LOGNAME=jimzhou OLDPWD=/Users/jimzhou/code/vim PATH='/opt/homebrew/opt/llvm/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Applications/VMware Fusion.app/Contents/Public:/Users/jimzhou/Programs:/opt/homebrew/Cellar/python@3.10:/opt/homebrew/Cellar/python@3.12' PWD=/Users/jimzhou/code/vim PYTHONIOENCODING=utf-8 SHELL=/bin/zsh SHLVL=1 SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.uzqRhgSAmr/Listeners TERM=xterm-256color TERM_PROGRAM=Apple_Terminal TERM_PROGRAM_VERSION=455 TERM_SESSION_ID=BCBB8226-679A-408D-A035-8416CD4775F6 TF_ALIAS=fuck TF_HISTORY=$'vim --version\nls\nvim e.vim\nvim --clean e.vim\nvim --clean e.vim\nls\ncd ..\ngit clone\nhttps://github.com/JimZhouZZY/vim.git\ngit clont https://github.com/JimZhouZZY/vim.git' TF_SHELL=zsh TF_SHELL_ALIASES=$'..=\'cd ..\'\ndir=ls\nf=fuck\nl=ls\nla=\'ls -la\'\nll=\'ls -l\'\nmatlab=/Applications/MATLAB_R2024b.app/bin/matlab\nmcscp=\'scp -i ~/code/minecraft/server_root.pem\'\nmcserver=\'sh ~/code/minecraft/ssh.sh\'\nminecraft=mc\nnethack=\'~/bin/nethack\'\npip=pip3.12\npip3=pip3.12\nproxy=$\'\\n export http_proxy=socks5://127.0.0.1:7890;\\n export https_proxy=socks5://127.0.0.1:7890;\\n export all_proxy=socks5://127.0.0.1:7890;\\n export no_proxy=socks5://127.0.0.1:7890;\\n export HTTP_PROXY=socks5://127.0.0.1:7890;\\n export HTTPS_PROXY=socks5://127.0.0.1:7890;\\n export ALL_PROXY=socks5://127.0.0.1:7890;\\n export NO_PROXY=socks5://127.0.0.1:7890;\'\npy=python3.12\npython=python3.12\npython3=python3.12\nrun-help=man\nsl=ls\nsudoit=\'sudo \'\nswap=swapit\nunproxy=$\'\\n unset http_proxy;\\n unset https_proxy;\\n unset all_proxy;\\n unset no_proxy;\\n unset HTTP_PROXY;\\n unset HTTPS_PROXY;\\n unset ALL_PROXY;\\n unset NO_PROXY\'\nunrar=unar\nwhich-command=whence' TMPDIR=/var/folders/qy/qxbg55_x1xlfgs6fstbczdzc0000gn/T/ USER=jimzhou XPC_FLAGS=0x0 XPC_SERVICE_NAME=0 __CFBundleIdentifier=com.apple.Terminal for
Updated vim.in and vim.out as @zzzyxwvut suggested. |
thanks! |
Specially handle commas within an enum block to correct indentation issue in #16289.
And improved test for indentation.