From 36d31c0a249396d9c62254bf8ed9b5a47b804f4a Mon Sep 17 00:00:00 2001 From: smjonas Date: Sat, 24 Dec 2022 14:31:42 +0100 Subject: [PATCH] feat(converter): map filetypes to YASnippet mode names and VSCode langs --- .../core/snipmate/yasnippet/converter.lua | 24 +++++++++++++++++-- .../core/snipmate/yasnippet/parser.lua | 2 +- .../core/vscode/converter.lua | 14 ++++++++++- 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/lua/snippet_converter/core/snipmate/yasnippet/converter.lua b/lua/snippet_converter/core/snipmate/yasnippet/converter.lua index 3e92a0d..583549d 100644 --- a/lua/snippet_converter/core/snipmate/yasnippet/converter.lua +++ b/lua/snippet_converter/core/snipmate/yasnippet/converter.lua @@ -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 diff --git a/lua/snippet_converter/core/snipmate/yasnippet/parser.lua b/lua/snippet_converter/core/snipmate/yasnippet/parser.lua index 8e9807f..ac0b182 100644 --- a/lua/snippet_converter/core/snipmate/yasnippet/parser.lua +++ b/lua/snippet_converter/core/snipmate/yasnippet/parser.lua @@ -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 diff --git a/lua/snippet_converter/core/vscode/converter.lua b/lua/snippet_converter/core/vscode/converter.lua index 3aa64bd..a014555 100644 --- a/lua/snippet_converter/core/vscode/converter.lua +++ b/lua/snippet_converter/core/vscode/converter.lua @@ -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 @@ -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