diff options
author | Eric Gaudet <gaudet.eric@gmail.com> | 2023-01-22 12:01:54 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-22 15:01:54 -0500 |
commit | 91c84d467bf98f44645eb3057202cb5da113d2d7 (patch) | |
tree | 31ba757ebfffc66720e674d05a146d21fc340522 | |
parent | d7ad7c0e3fecaf2c837c1017b385ddef0721911b (diff) | |
download | lite-xl-plugins-91c84d467bf98f44645eb3057202cb5da113d2d7.tar.gz lite-xl-plugins-91c84d467bf98f44645eb3057202cb5da113d2d7.zip |
Add smartopenselected plugin (#176)
Try to open the selection as a filename or path in the project.
Path separators are used to replace ".", so import path can also be found.
Co-authored-by: Eric Gaudet <egaudet@thefind.com>
-rw-r--r-- | manifest.json | 7 | ||||
-rw-r--r-- | plugins/smartopenselected.lua | 48 |
2 files changed, 55 insertions, 0 deletions
diff --git a/manifest.json b/manifest.json index 1b5697d..b8c0480 100644 --- a/manifest.json +++ b/manifest.json @@ -1121,6 +1121,13 @@ "mod_version": "3" }, { + "description": "Opens the selected filename or path in project. Useful to open imports.", + "version": "0.1", + "path": "plugins/smartopenselected.lua", + "id": "smartopenselected", + "mod_version": "3" + }, + { "description": "Smooth caret animation *([gif](https://user-images.githubusercontent.com/20792268/139006049-a0ba6559-88cb-49a7-8077-4822445b4a1f.gif))*", "version": "0.1", "path": "plugins/smoothcaret.lua", diff --git a/plugins/smartopenselected.lua b/plugins/smartopenselected.lua new file mode 100644 index 0000000..dfdb96d --- /dev/null +++ b/plugins/smartopenselected.lua @@ -0,0 +1,48 @@ +-- mod-version:3 +local core = require "core" +local command = require "core.command" +local keymap = require "core.keymap" +local common = require "core.common" +local contextmenu = require "plugins.contextmenu" + + +command.add("core.docview!", { + ["smart-open-selected:smart-open-selected"] = function(dv) + local doc = dv.doc + if not doc:has_selection() then + core.error("No text selected") + return + end + + local text_orig = doc:get_text(doc:get_selection()) + text_orig = text_orig:match( "^%s*(.-)%s*$" ) + + -- transform java/python imports to paths + local text_path, num = text_orig:gsub("[.]", PATHSEP) + + -- keep the last . in case the path contains a file extension + local text_keep_extension, num = text_orig:gsub("[.]", PATHSEP, num - 1) + + -- trim whitespace from the ends + + for dir, item in core.get_project_files() do + if item.type == "file" and ( + string.find(item.filename, text_orig) + or string.find(item.filename, text_path) + or string.find(item.filename, text_keep_extension) + ) then + local path = (dir == core.project_dir and "" or dir .. PATHSEP) + local filepath = common.home_encode(path .. item.filename) + core.root_view:open_doc(core.open_doc(common.home_expand(filepath))) + end + end + end, +}) + + +contextmenu:register("core.docview", { + { text = "Smart Open Selection", command = "smart-open-selected:smart-open-selected" } +}) + + +keymap.add { ["ctrl+shift+alt+p"] = "smart-open-selected:smart-open-selected" } |