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

feat: allow rules to be treesitter context aware (#423) #424

Merged
merged 2 commits into from Jan 21, 2024

Conversation

kamalsacranie
Copy link
Contributor

@kamalsacranie kamalsacranie commented Jan 18, 2024

When a rule is defined which calls the :with_context() method, has a
specified filetype, and is operating in a buffer with a treesitter
parser attached, the rule will only execute iff the treesitter language
at the cursor matches one of the filetypes specified in the initial rule
definition.

If there are no specified filetypes, of there is no parser attached to
the current buffer, the rule executes as normal


For the tests, I created a sample.md file which I then use with the rule to test. It was required to make a small modification to Test_withfile. We needed to carry out a full parse of the buffer tree which returns a language tree with the injected child trees. The test check if the rules work exclusively in the specified grammars/filetypes.

@kamalsacranie kamalsacranie changed the title feat: allow rules to be treesitter context aware feat: allow rules to be treesitter context aware #423 Jan 18, 2024
@kamalsacranie kamalsacranie changed the title feat: allow rules to be treesitter context aware #423 feat: allow rules to be treesitter context aware Jan 18, 2024
@kamalsacranie kamalsacranie changed the title feat: allow rules to be treesitter context aware feat: allow rules to be treesitter context aware (#423) Jan 18, 2024
@kamalsacranie kamalsacranie force-pushed the ts-context branch 2 times, most recently from 0757ff3 to 3534bfd Compare January 18, 2024 15:05
@windwp
Copy link
Owner

windwp commented Jan 19, 2024

Thank you for your PR.

I expect it can handle a different behavior of existing rule with inline file type
sample with rule f''' on python.

 ```python
  f''' # type it on python it will become f'''|'''
 ```

or rule <!-- --> . so it can only work on markdown and html

Yeah, I know it needs a lot of refactoring.

That's why I suggest using a conditional behavior. Your tree sitter context only of affecting one rule with the character *, so you only need to add a condition like this:

Rule('*', '*', { 'markdown' }):with_pair(ts_conds.is_not_inside_context())

@kamalsacranie
Copy link
Contributor Author

Yours is a much cleaner implementation. I think would be good to have

Rule("*", "*"):with_pair(ts_conds.is_inside_context({"markdown"}))
Rule('f"""', '"""'):with_pair(ts_conds.is_inside_context({"python"}))

That way, we can have the behaviour you mentioned.

I expect it can handle a different behavior of existing rule with inline file type sample with rule f''' on python.

 ```python
  f''' # type it on python it will become f'''|'''
 ```

I think if we specify the filetype, the rule will not run when we are not in the filetype specified. I.e. if we did Rule(..., {"python"}), then the rule would not run in the markdown code block because we aren't in a python file (is this correct?). Let me know if you would prefer it to be like you mentioned before (Rule('*', '*', { 'markdown' }):with_pair(ts_conds.is_not_inside_context()))

@windwp
Copy link
Owner

windwp commented Jan 19, 2024

but the problem is if we use condition then that rule will not load to buffer or create a mapping on buffer

if you change this pr to add a new condition. i will merge.
I will thinking about how to apply it to exisiting rules later.

@kamalsacranie
Copy link
Contributor Author

Okay. I've implemented it how you suggested

When a rule is defined with the `:with_context()` method, has a
specified filetype, and is operating in a buffer with a treesitter
parser attached, the rule will only execute iff the treesitter language
at the cursor matches one of the filetypes specified in the initial rule
definition.

> If there are no specified filetypes, of there is no parser attached to
> the current buffer, the rule executes as normal
- Add 'ts_context markdown `*` success md_context'
- Add 'ts_context codeblock `*` fail js_context'
@windwp
Copy link
Owner

windwp commented Jan 21, 2024

ok, thank

@windwp windwp merged commit eac31b4 into windwp:master Jan 21, 2024
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

Successfully merging this pull request may close these issues.

None yet

2 participants