Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for compound filetypes #142

Closed
wincent opened this issue Aug 31, 2021 · 16 comments
Closed

Support for compound filetypes #142

wincent opened this issue Aug 31, 2021 · 16 comments

Comments

@wincent
Copy link

wincent commented Aug 31, 2021

This is something that UltiSnips offers when working with compound or "dotted" filetypes (eg. foo.bar; see :h 'filetype').

Most usefully, it does this:

UltiSnips understands Vim's dotted filetype syntax. For example, if you define a dotted filetype for the CUDA C++ framework, e.g. :set ft=cuda.cpp, then UltiSnips will search for and activate snippets for both the cuda and cpp filetypes.

A common use case for this is to have snippets for your "main" language (say, typescript) and then define additional specific snippets for test files (using a filetype of, say, jest); the test files have a 'filetype' of typescript.jest and so get both kinds of snippets, but non-test files just get the typescript ones. Similar analogies exist for other languages (eg. for ruby you might use RSpec for tests and so add spec to your filetype — ie. ruby.spec — so that you can get test-specific snippets there).

Perhaps a little less usefully, it also allows you to explicitly merge filetypes:

The UltiSnipsAddFiletypes command allows for explicit merging of other snippet filetypes for the current buffer. For example, if you edit a .rst file but also want the Lua snippets to be available you can issue the command

:UltiSnipsAddFiletypes rst.lua

I personally haven't used that second feature at all, but I used the first feature quite a bit.

Anyway, not sure if this is possible with LuaSnip right now so just wanted to pop this in as a feature request (or perhaps it already is possible and could be added to the docs?).

wincent added a commit to wincent/wincent that referenced this issue Aug 31, 2021
Bring back some snippets like the ones I used to have with Ultisnips
(and technically still do over in my .vim files):

    https://github.com/wincent/wincent/blob/b88ca40893ae/aspects/nvim/files/.vim/ultisnips/spec.snippets

AFAICT there is no obvious way to conditionally enable this in files
with compound filetypes, so just adding this for all Ruby files for now.

I asked upstream about whether this is possible:

    L3MON4D3/LuaSnip#142
@L3MON4D3
Copy link
Owner

L3MON4D3 commented Aug 31, 2021

Ouh I think right now dotted filetypes actually break luasnip a bit (we assume that there's only one filetype in ft, no dots), so there'd be no snippets from any filetype except global ones.
Supporting compound filetypes should be pretty easy, I'll add that

@L3MON4D3 L3MON4D3 reopened this Aug 31, 2021
@L3MON4D3
Copy link
Owner

(missclick :|)

@leiserfg
Copy link
Contributor

@L3MON4D3 my pr is more complete (I also fixed it for the vscode lazy loading)

@wincent
Copy link
Author

wincent commented Aug 31, 2021

Thank you both, @L3MON4D3 @leiserfg!

wincent added a commit to wincent/wincent that referenced this issue Aug 31, 2021
Motivation; get this fix:

    L3MON4D3/LuaSnip#142

But it turns out that there have been a _lot_ of changes since I last
updated; if anything breaks, I will follow up:

* aspects/nvim/files/.config/nvim/pack/bundle/opt/LuaSnip 72323c1...9a8cf5f (261):
  > Merge pull request #143 from leiserfg/multi-ft
  > Format with stylua
  > fix: account for compound filetypes (closes #142).
  > Update README.md
  > Format with stylua
  > Fix parser, for real now.
  > Format with stylua
  > Fix slight regression with variables in lsp-parsed snippets.
  > Fixup(d2aa43c): BREAKING, parsing has become more strict.
  > Format with stylua
  > Provide option to set better `assemble`-function for nested snippets.
  > Format with stylua
  > Merge branch 'master' of github.com:L3MON4D3/LuaSnip
  > lsp: correctly parse brackets (closes #141) and split iNode-text.
  > fix(parser): remove escape-chars everywhere.
  > Merge pull request #140 from carlitux/patch-2
  > Set pos to nil for better docstring if queried manually.
  > Format with stylua
  > Improve get_docstring().
  > Typo.
  > Format with stylua
  > Account for selecting empty lines (only) (closes #139).
  > Use `rawget` to get position.
  > Merge pull request #138 from leiserfg/handle_emtpy_snippets
  > Parser: ${n:sometext} can also be repetitions of previous insertNodes.
  > Try expanding autosnippets if pum is visible, too.
  > Format with stylua
  > Use tokenizer to create helper-snippet for regex-triggers.
  > Implement tokenizer for patterns.
  > Format with stylua
  > pcall `get_text`, {""} on error. Don't error on `str_utfindex`.
  > Try to prevent jump_from from erroring due to deleted snippets.
  > Auto generate docs
  > Format with stylua
  > Merge branch 'insert_node_event_callbacks'
  > Add <Plug>-mapping for previous choice (Closes #131).
  > Format with stylua
  > Provide setting for delete-check autocommands.
  > Recommend `InsertEnter` for `region_check_events`
  > Make bytecol_to_utfcol behave correctly (used to return three values).
  > Auto generate docs
  > Format with stylua
  > Small changes+improvements for the enter/leave events:
  > Auto generate docs
  > Rename AUTOCOMMANDS to EVENTS.
  > Auto generate docs
  > Format with stylua
  > No need to input_leave the generated snippet in dynamicNode.
  > Trigger autocommands on entering/leaving nodes.
  > Add function to check if the current snippet was deleted+<Plug>-mapping.
  > Merge pull request #126 from leiserfg/generic-package-name
  > Auto generate docs
  > Format with stylua
  > Document indent_snippet_node and some usages for it.
  > Remove prefix_snippet_node, it's really just a specification of ISN.
  > Re-implement changing choice backwards.
  > Prevent region check for autotriggered snippets too.
  > Each choice gets a pointer to the next.
  > Rename choiceNode.inner to choiceNode.active_choice, more expressive.
  > Clear snippets' own marks on exit.
  > Rename vars that store choiceNodes to reflect that.
  > Format with stylua
  > Disable region check while jumping.
  > Make sure node isn't nil before indexing.
  > Check for specific node+also check following snippets.
  > Leave exit node when next snippet is expanded, only compare lines for region.
  > Mention setting in README.
  > Add config for specifying when to do region check, defaults to never.
  > Fix minor bugs with exit_out_of_region_snippet.
  > Fix small issues with exitNode.
  > Fix bug caused by new jump_from/into-behaviour.
  > Add function to leave active snippet if its' region is left.
  > Make no_move actually funcitional.
  > Add snippet.snippet, contains 'outer' (ie. the user-triggered) snippet.
  > Have jump_into/jump_leave return the new active node.
  > Merge pull request #124 from leiserfg/clean_lazy
  > Auto generate docs
  > Fix lazy-loading.
  > Add more details for loading vscode-packaged snippets.
  > Format with stylua
  > Fix/improve update_dependents-behaviour.
  > Clean up a bit.
  > Reset active upon exit()ing, only pop active choice if this was active.
  > Simplify choiceNodes' init-process.
  > Merge pull request #121 from L3MON4D3/static_text_for_snippets
  > Ignore and correctly format lines on which stylua fails.
  > Format with stylua
  > Fix regression: *move_on_mark_*-functions were removed, typo(?) in util.mark.
  > Format with stylua
  > Remove print's.
  > Changes nothing functionally, but clearer.
  > After parsing, the positions of insertNodes are not 'dense', fix it.
  > Inserting a missing `i(0)` is handled by snippet-constructor.
  > Auto generate docs
  > Format with stylua
  > Document expected layout of directories loaded via vscode-loader.
  > Format with stylua
  > Separate expanding tabs in text and indenting; make both more generic.
  > Fix expand_tab-implementation.
  > Always initialize static_text for insert and textNode.
  > Format with stylua
  > Fix unlink_current, correctly set active node.
  > Set Luasnip_active_choice correctly after exit()ing choiceNode.
  > Format with stylua
  > Merge pull request #114 from leiserfg/extra-vars
  > Format with stylua
  > Differentiate between insert and exitNodes(`i(0)`).
  > Call insertNodes' input_leave function for i(0).
  > Auto generate docs
  > Format with stylua
  > Document new behaviour of i(0).
  > Format with stylua
  > Do insertNodes' input_enter for i(0).
  > Fix TM_LINE_INDEX/NUMBER, it should be a string.
  > Format with stylua
  > Explicitly open folds after moving, doesn't seem to happen automatically.
  > Auto generate docs
  > Format with stylua
  > Mention passive extending active and highlight groups in doc.
  > Fix overriding-behaviour.
  > Fix clear_invalid, don't have it override active with passive.
  > Use user-defined Luasnip*Node{Active,Passive} as highlight when defined.
  > Merge default into user-defined table.
  > Format with stylua
  > Replace link to matrix room with icon.
  > Format with stylua
  > Clarify arguments to util.put.
  > Use byte-position of mark to nvim_buf_set_text().
  > Expose function to get byte-position of mark.
  > Format with stylua
  > Default ext_opts-table to {}.
  > Auto generate docs
  > Format with stylua
  > Add example for ext_opts, fix jdoc-snippet.
  > Rename prio_increase to ext_prio_increase.
  > Add documentation for ext_opts.
  > Correctly pass ext_opts with higher prios for inner snippets.
  > Config: include priority+functions to modify.
  > Add appropriate exit()-functions to dynamic and choice.
  > Use copy of passed table.
  > Fix dynNode:set_rgrav, after changing textNode, grav is default-false.
  > Fill missing values in ext_opts.
  > Don't unnecessarily update extmark.
  > Change ext_opts on entering/leaving.
  > Correctly set type and opts for snippet.
  > Rename change_rgravs to update_rgravs.
  > DO change gravities for textNodes, prevents some bug+more uniform.
  > Enter node in both paths.
  > Delete marks on exit.
  > Use two functions to set gravs or other opts.
  > Set snippets' mark from outside also.
  > Format with stylua
  > Mention nvim-cmp and cmp_luasnip in README.
  > Format with stylua
  > Merge pull request #106 from amenonsen/patch-3
  > Format with stylua
  > Merge pull request #105 from amenonsen/patch2
  > Format with stylua
  > Yeeeeeaaaaah no, not really anymore
  > Format with stylua
  > Mention autosnippets in README.
  > Format with stylua
  > Merge branch 'autotrigger'
  > Format with stylua
  > Fix: Adding a i(0) if none was specified.
  > Don't set correctly and then overwrite with old values :/
  > As the mark-id isn't copied anymore, it can be set later in put_initial()
  > Implement clear-function, fix bug with raw_pos.
  > Print gravities in snippet:dump().
  > Store ext_opts in snippet, pass to children; Change marks for dyn/choice
  > Add ext_opts to config.
  > Use raw, not utf-adjusted position for initializing extmarks.
  > Don't set positions in opts.
  > Default-init marks with nil.
  > Fix set_mark_rgrav-function, use default-version everywhere.
  > Actually use new marks in snippets.
  > Most likely, there won't be any updates to pos, and if, then to both.
  > Marks are objects; Simplify extmark_rgrav-setting.
  > Add table for node-names, shift indices to match name.
  > Refactor types to use enum.
  > Auto generate docs
  > Mention mappings in DOC, refer to README.
  > Auto generate docs
  > Merge pull request #104 from amenonsen/patch-1
  > Mention new config-option in README.
  > Auto generate docs
  > Document SELECT-behaviour and lsp-parsing.
  > Provide example mappings for lua and vimscript.
  > Link to lsp-snippet spec.
  > Format with stylua
  > Only remove indent for TM_SELECTED_TEXT.
  > Enable new vars in env.
  > Correctly populate selection.
  > Create all types of indent in store_selection().
  > Modify store_selection to get all lines at once.
  > Allow node.pos to be nil, mimic vscodes' behaviour for $TM_SELECTED_TEXT.
  > Should be an insertNode.
  > Add new snippetNodes prefixSnippetNode and indentSnippetNode
  > Refactor population of args for function/dynamicNodes.
  > Improve autowrapping for snippets, fix it for snippetNodes.
  > Merge pull request #102 from leiserfg/find_vimrc
  > Auto generate docs
  > Specify what the text looks like when executing condition-function.
  > Auto generate docs
  > Maybe also document condition-function.
  > Format with stylua
  > Change snippet:match(...) for regex-triggers, parentheses can mess up patterns.
  > Reuse mark-id if possible.
  > Refactor snippet:indent() slightly.
  > Clean up unneeded args.
  > Indent + populate snippetNodes' env in put_initial().
  > Auto generate docs
  > Format with stylua
  > Explicitly mention text-passing behaviour in docs.
  > BREAKING CHANGE: Pass dedented text to function/dynamicNodes, indent returned text for functionNodes.
  > Merge pull request #94 from leiserfg/fix-of-by-one
  > Merge pull request #93 from leiserfg/use-named-register
  > Format with stylua
  > Reset Visual after use.
  > Return {} instead of "" on empty visual.
  > Prepare for storing prio,hl_group,... in node.mark.
  > Format with stylua
  > Correctly check for empty table ({} != {} in lua);
  > Rewrite extmarks to work more consistently.
  > Update parsed nodes.
  > Fix node-gravities for exitNodes'.
  > Remove prints', fix enter_node.
  > Use only one node to mark beginning and end of node.
  > Merge pull request #92 from leiserfg/select
  > If lambda returns nil, use default of "" to prevent errors.
  > Format with stylua
  > Add dynamic lambdas+examples.
  > Format with stylua
  > Add nonempty-node. Inserts text if arg-node is not empty.
  > Add some examples for `match()`.
  > Adjust default-value for `match()`
  > Merge pull request #86 from leiserfg/match_with_lambda
  > BREAKING CHANGE: Move pseudo-nodes from ls.util.functions to ls.extras.
  > Quickly shuffle around signature of `match()`.
  > Merge pull request #84 from leiserfg/match_with_lambda
  > Auto generate docs
  > Merge pull request #83 from praveendhawan/patch-1
  > Auto generate docs
  > Format with stylua
  > BREAKING CHANGE: Default `wordTrig` to true, not false.
  > Add expand and expandable function+vim-versions (close #79).
  > Luasnip_current_nodes may (somehow) be nil, check before indexing.
  > Format with stylua
  > Add partial function+example (closes #72).
  > Auto generate docs
  > Replace table with list, md2vim doesn't like tables.
  > Auto generate docs
  > Fix table in Doc.
  > Merge pull request #69 from leiserfg/md-doc
  > Alias set_config to setup.
  > Merge pull request #29 from tjdevries/update_config_correctly
  > Initialize function+dynamicNodes when cildren of choiceNode.
  > Format with stylua
  > Add Examples for recently added nodes.
  > Repeat doesn't need lambdas.
  > Merge pull request #67 from leiserfg/lambda-util
@shellRaining
Copy link

Very offensive, but may I ask where can I find the relevant configuration information? I am using jest and want to separately configure code snippets for files like *.spec.ts. However, besides this issue, I haven't seen any other information.

@leiserfg
Copy link
Contributor

@shellRaining we use the file type and not the file extension to find the set of snippets to use. So you have to use vim.filetype.add to make nvim to assign a custom filetype to those (or an autocmd).

@shellRaining
Copy link

@leiserfg I have set the filetype of *.spec.ts by using code below

ft.add({
    pattern = {
        [".*.spec.ts"] = "jest",
    },
})

and it print the expected filetype jest, however, the code not highlight as typescript file, then I search for config mult filetype for one file, and no useful information, may I ask for some solutions about how to setup snippets for *.spec.ts while treat this file as a normal ts file (and it's better that the test snippets not load in a normal ts file)

@leiserfg
Copy link
Contributor

try with

ft.add({
    pattern = {
        [".*.spec.ts"] = "jest.typescript",
    },
})

@shellRaining
Copy link

@leiserfg thank you, the high light does work now, but snippet still not work properly, when use OpenUserSnippetFile cmd, Neovim open the jest.typescript.json in my snippets project, and I set the package.json below

{
  "path": "./jest.typescript.json",
  "language": [
    "spec.ts",
    "spec.js",
    "jest.javascript",
    "jest.typescript"
  ]
},

I'm sure that the snippets is correct, because when et the language to "typescript", this work as expected

@leiserfg
Copy link
Contributor

It seems like there was a change, on one of the rewrites that partially broke this, in the past it worked as you have it currently configured. Now it should work fine if you set the language in the json list languages to be spec only.

@leiserfg
Copy link
Contributor

leiserfg commented Nov 10, 2023

In short:

ft.add({
    pattern = {
        [".*.spec.ts"] = "jest.typescript"
    },
})

plus

{
  "path": "./jest.typescript.json",
  "language": [
    "spec"
  ]
},

@leiserfg
Copy link
Contributor

leiserfg commented Nov 10, 2023

Which is technically matching the way nvim behaves as it see ft with dots not as more specific ft but as having several ft in the same file, so the current behavior is more correct in that sense.

@shellRaining
Copy link

still not work after set the language to spec😢. The relevant configurations are in this commit shellRaining/nvim@db37ddb.

@L3MON4D3
Copy link
Owner

Shouldn't the language in the package.json be jest? Or did you want to account for ft.add being run after the load/lazy_load?

@shellRaining
Copy link

@L3MON4D3 thank you very much!!! work properly 😄

@leiserfg
Copy link
Contributor

Shouldn't the language in the package.json be jest? Or did you want to account for ft.add being run after the load/lazy_load?

Sure, I had a Lapsus mentis.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants