Open
Conversation
Added missing version information
dabc061 to
decbf37
Compare
Contributor
|
This is really cool. Do you mind sharing the |
Contributor
Author
|
The code is really old and it "just worked" so I never bothered making it better or changing it. Anyway, here it is: snippet_helper.lualocal line_begin = require("luasnip.extras.expand_conditions").line_begin
local module = {}
local in_docstring = function()
local current_node = vim.treesitter.get_node({ buffer = 0 })
if not current_node then
return false
end
local type_name = current_node:type()
if vim.bo.filetype == "python" then
return type_name == "string_content"
end
vim.notify(
'Type name"' .. type_name .. "\" is unknown. Cannot check if we're in a docstring.",
vim.log.levels.ERROR
)
return false
end
-- Reference: https://snippets.bentasker.co.uk/page-1706031025-Trim-whitespace-from-beginning-of-string-LUA.html
local ltrim = function(text)
return text:match "^%s*(.*)"
end
local strip_spaces = function(text)
return text:gsub("%s+", "")
end
local and_ = function(...)
local functions = { ... }
return function(...)
for _, function_ in ipairs(functions) do
if not function_(...) then
return false
end
end
return true
end
end
local is_line_beginning = function(line_to_cursor)
return strip_spaces(line_to_cursor) == ""
end
local is_source_beginning = function(trigger)
local wrapper = function(line_to_cursor)
return line_begin(ltrim(line_to_cursor), trigger) ~= nil
end
return wrapper
end
local or_ = function(...)
local functions = { ... }
return function(...)
for _, function_ in ipairs(functions) do
if function_(...) then
return true
end
end
return false
end
end
module.in_docstring = in_docstring
module.is_line_beginning = is_line_beginning
module.is_source_beginning = is_source_beginning
module.and_ = and_
module.or_ = or_
return module |
Contributor
|
Thank you! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
A (sort of) continuation of #201
I integrate Neogen + LuaSnip into one with this snippet:
Click to expand
(This later gets included to `luasnip.add_snippets("python", those_snippets_above)
With this, I can just write docstrings naturally like
Args:and Neogen auto-expands the arguments for me.The end result looks like this:
2024-09-02.16-02-25.mp4
2024-09-02.16-02-39.mp4
(Sorry the recordings are a bit messed up)
Anyway being able to explicitly ask for the snippet engine means that a user can default to a different snippet engine in their configuration but run a different engine on-demand. This is useful as someone who wants to move to native LSP snippets someday.