Skip to content

change: use starting column when using *_current_* apis - #136

Merged
numToStr merged 1 commit into
numToStr:masterfrom
declancm:master
Apr 1, 2022
Merged

numToStr merged 1 commit into
numToStr:masterfrom
declancm:master

Conversation

@declancm

Copy link
Copy Markdown
Contributor

When using an API function on a single line with embedded code, the commentstring detected using treesitter is not correct when using the cursor position. To fix this, I am changing the column position being parsed when vmode is set to nil.

@declancm

Copy link
Copy Markdown
Contributor Author

@numToStr Is there any reason the gcc keymap uses g@$ instead of g@0?
By using the start column, I think it would provide more accurate commentstrings as it's actually parsing the section of the line where the comment will be added. For example:

-- When using gcc on the second line, this:
vim.cmd [[
echo 'test']]

-- becomes this:
vim.cmd [[
" echo 'test']]

-- instead of this (the original behaviour):
vim.cmd [[
-- echo 'test']]

-- Lua was detected at the end of the line and then the
-- lua commentstring got inserted into the vimscript.

I'm not sure if there are maybe some situations you prefer g@$ instead which is why it was selected?

@numToStr

numToStr commented Mar 31, 2022 •

Copy link
Copy Markdown
Owner

@numToStr Is there any reason the gcc keymap uses g@$ instead of g@0? By using the start column, I think it would provide more accurate commentstrings as it's actually parsing the section of the line where the comment will be added

@declancm Nope, there is nothing particular reasoning behind using g@$ but this was the first combination that worked on empty lines. Though I don't remember trying g@0 maybe you can give it a try and report.

If g@0 works on empty lines then gcc will be similar to toggle_current_linewise. BTW Did you happen to try this PR on empty lines?

@declancm

Copy link
Copy Markdown
Contributor Author

@numToStr Yes I tested the PR and it works on empty lines :) .

I'm struggling to make the gcc map work for every situation cleanly as you were right about needing g@$ for when the cursor location is 0. I believe this is close to making gcc match the performance of toggle_current_linewise, but it's very long and a bit gross and still not as good:

[[v:count == 0 ? (nvim_win_get_cursor(0)[1] == 0 ? (getline('.') == '' ? '<CMD>lua require("Comment.api").call("toggle_current_linewise_op")<CR>g@$' : '<CMD>lua require("Comment.api").call("toggle_current_linewise_op")<CR>g@l') : '<CMD>lua require("Comment.api").call("toggle_current_linewise_op")<CR>g@0') : '<CMD>lua require("Comment.api").call("toggle_linewise_count_op")<CR>g@$']],

The api function can be used directly to fix the performance difference but idk if you want that:

[[v:count == 0 ? '<CMD>lua require("Comment.api").toggle_current_linewise()<CR>' : '<CMD>lua require("Comment.api").call("toggle_linewise_count_op")<CR>g@$']],

@numToStr

numToStr commented Apr 1, 2022

Copy link
Copy Markdown
Owner

I don't think the added complexity in gcc is worth it when anyone can fix this by changing the code slightly.

-- Original
vim.cmd [[
echo 'test']]

-- After
vim.cmd [[
-- echo 'test']]

-- After changing the code
vim.cmd [[
" echo 'test'
]]

@numToStr numToStr changed the title Closes #135; fix: detect correct ts cstring with api functions change: use starting column in range when using *_current_* api functions Apr 1, 2022
@numToStr numToStr changed the title change: use starting column in range when using *_current_* api functions change: use starting column when using *_current_* api functions Apr 1, 2022
@numToStr numToStr changed the title change: use starting column when using *_current_* api functions change: use starting column when using *_current_* apis Apr 1, 2022
@numToStr
numToStr merged commit 546c487 into numToStr:master Apr 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

API toggle_current_linewise not working with single line vim.cmd [[<stuff>]]

2 participants