[Feat] support highlighting no extension file (#70)

pull/72/head
The Mist 2024-04-05 23:23:17 +08:00 committed by GitHub
parent 45804b3341
commit 8bd3cd89e0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 70 additions and 36 deletions

59
lua/codesnap/config.lua Normal file
View File

@ -0,0 +1,59 @@
local visual_utils = require("codesnap.utils.visual")
local path_utils = require("codesnap.utils.path")
local string_utils = require("codesnap.utils.string")
local static = require("codesnap.static")
local list_utils = require("codesnap.utils.list")
local table_utils = require("codesnap.utils.table")
local config_module = {}
local assets_folder = static.cwd .. "/assets"
-- Get extension of cureent file
local function get_file_info()
local file_path = vim.fn.expand("%:p")
local filename, extension = string.match(file_path, "([^\\/%.]+)%.?([^\\/%.]*)$")
return string_utils.convert_empty_to_nil(filename), string_utils.convert_empty_to_nil(extension)
end
-- Some files have no extension, but they still need to highlighting correctly,
-- in this case, use filename instead of extension to highlighting code
-- e.g. Dockerfile, Pipefile
local function parse_file_extension_by_highlighting_file_presets(filename, extension)
local lowercase_filename = string.lower(filename)
return extension or lowercase_filename
end
local function parse_extension(specify_extension)
local filename, file_extension = get_file_info()
return specify_extension
or file_extension
or parse_file_extension_by_highlighting_file_presets(filename, file_extension)
end
function config_module.get_config(specify_extension)
local code = visual_utils.get_selected_text()
local extension = specify_extension or parse_extension(specify_extension)
if string_utils.is_str_empty(code) then
error("No code is selected", 0)
return
end
if string_utils.is_str_empty(extension) then
error("Cannot detect current filetype", 0)
end
return table_utils.merge({
code = code,
extension = extension,
fonts_folder = assets_folder .. "/fonts",
themes_folder = assets_folder .. "/themes",
theme = "base16-onedark",
file_path = static.config.has_breadcrumbs and path_utils.get_relative_path() or "",
}, static.config)
end
return config_module

View File

@ -1,17 +1,7 @@
local static = require("codesnap.static") local static = require("codesnap.static")
local table_utils = require("codesnap.utils.table") local table_utils = require("codesnap.utils.table")
local string_utils = require("codesnap.utils.string") local string_utils = require("codesnap.utils.string")
local visual_utils = require("codesnap.utils.visual") local config_module = require("codesnap.config")
local path_utils = require("codesnap.utils.path")
local assets_folder = static.cwd .. "/assets"
local function get_extension()
local file_path = vim.fn.expand("%:p")
local file_extension = string.match(file_path, "%.([^%.]+)$")
return file_extension
end
local main = { local main = {
cwd = static.cwd, cwd = static.cwd,
@ -22,31 +12,8 @@ function main.setup(config)
static.config = table_utils.merge(static.config, config == nil and {} or config) static.config = table_utils.merge(static.config, config == nil and {} or config)
end end
local function get_config(specify_extension)
local code = visual_utils.get_selected_text()
local extension = specify_extension or get_extension()
if string_utils.is_str_empty(code) then
error("No code is selected", 0)
return
end
if string_utils.is_str_empty(extension) then
error("Cannot detect current filetype", 0)
end
return table_utils.merge({
code = code,
extension = extension,
fonts_folder = assets_folder .. "/fonts",
themes_folder = assets_folder .. "/themes",
theme = "base16-onedark",
file_path = static.config.has_breadcrumbs and path_utils.get_relative_path() or "",
}, static.config)
end
function main.copy_into_clipboard(extension) function main.copy_into_clipboard(extension)
require("generator").copy_into_clipboard(get_config(extension)) require("generator").copy_into_clipboard(config_module.get_config(extension))
vim.cmd("delmarks <>") vim.cmd("delmarks <>")
vim.notify("Save snapshot into clipboard successfully") vim.notify("Save snapshot into clipboard successfully")
end end
@ -59,7 +26,7 @@ function main.save_snapshot(extension)
) )
end end
require("generator").save_snapshot(get_config(extension)) require("generator").save_snapshot(config_module.get_config(extension))
vim.cmd("delmarks <>") vim.cmd("delmarks <>")
vim.notify("Save snapshot in " .. static.config.save_path .. " successfully") vim.notify("Save snapshot in " .. static.config.save_path .. " successfully")
end end

View File

@ -16,4 +16,12 @@ function string_util.is_str_empty(target)
return target == nil or target == "" return target == nil or target == ""
end end
function string_util.convert_empty_to_nil(target)
if target == "" then
return nil
else
return target
end
end
return string_util return string_util