Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions lua/snippet_converter/core/snipmate/yasnippet/converter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,17 +61,37 @@ local HEADER_STRING = [[

]]

-- The first table entry is the child mode, the others are parent modes
local ft_to_modenames = {
sh = { "shell", parent_modes = { "shell-script", "sh", "sh-script" } },
}

-- Takes a list of converted snippets for a particular filetype,
-- separates them by newlines and exports them to a file.
-- @param converted_snippets table(trigger: string, body: string) A list of snippet tables where each item is a snippet table to be exported
-- @param mode_name string The filetype of the input snippets
-- @param output_dir string The absolute path to the directory to write the snippets to
M.export = function(converted_snippets, filetype, output_path)
-- @param context []? #A table of additional snippet contexts optionally provided the source parser (e.g. extends directives from UltiSnips)
M.export = function(converted_snippets, filetype, output_path, context)
local modes = ft_to_modenames[filetype]
local child_mode = modes and modes[1]

for _, snippet in ipairs(converted_snippets) do
local snippet_lines = HEADER_STRING .. snippet.body
local output_file_path = ("%s/%s-mode/%s"):format(output_path, filetype, snippet.trigger)
local output_file_path = ("%s/%s-mode/%s"):format(output_path, child_mode, snippet.trigger)
io.write_file(vim.split(snippet_lines, "\n"), output_file_path)
end

-- Create a .yas-parents file to share snippets
if child_mode then
local parent_modes = vim.tbl_map(function(mode_name)
return mode_name + "-mode"
end, modes.parent_modes)

local yas_parents_path = ("%s/%s-mode/.yas-parents"):format(output_path, child_mode)
io.write_file({ table.concat(parent_modes, " ") + "\n" }, yas_parents_path)
end

return output_path
end

Expand Down
2 changes: 1 addition & 1 deletion lua/snippet_converter/core/snipmate/yasnippet/parser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ M.get_lines = function(file)
end

local mode_name_to_fts = {
web = { "html" },
ess = { "r", "S-Plus", "SAS", "julia", "stata" },
web = { "html" },
}

---@return number the updated number of snippets that have been parsed
Expand Down
14 changes: 13 additions & 1 deletion lua/snippet_converter/core/vscode/converter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,10 @@ M.export = function(converted_snippets, filetypes, output_dir, _)
return output_path
end

local ft_to_langs = {
sh = { "shell_script" }
}

-- @param context []? @A table of additional snippet contexts optionally provided the source parser (e.g. extends directives from UltiSnips)
M.post_export = function(template_name, filetypes, output_path, context, template_opts)
if template_opts and not template_opts.generate_package_json then
Expand All @@ -178,7 +182,15 @@ M.post_export = function(template_name, filetypes, output_path, context, templat
return ft ~= "package"
end, filetypes)

local json_string = get_package_json_string(template_name, filetypes, context.langs_per_filetype or {})
-- This would map { ts = { "ts", "js" } } to { ts = { "typescript", "javascript" } }
local langs_map = context.langs_per_filetype or {}
for ft, langs in pairs(langs_map) do
for i, sub_ft in ipairs(langs) do
langs_map[ft][i] = ft_to_langs[sub_ft]
end
end

local json_string = get_package_json_string(template_name, filetypes, langs_map)
local lines = export_utils.snippet_strings_to_lines { json_string }
io.write_file(lines, io.get_containing_folder(output_path) .. "/package.json")
end
Expand Down