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

Highligths and queries #2

Open
danjessen opened this issue Nov 22, 2023 · 22 comments
Open

Highligths and queries #2

danjessen opened this issue Nov 22, 2023 · 22 comments

Comments

@danjessen
Copy link

I'm just adding this as a note. I haven't played with tree-sitter and tree-sitter queries yet. But I might see if I can find time to pitch in. I love the effort put in. But there is still some things to improve.

Screenshot 2023-11-22 at 08 10 29

Remarks:

Built-in Functions - Essential

Built-in Functions - Non-essentials

Built-in Config Files (Non-essentials?)

Built-in Basics

Custom Smarty Tags

Injections (Self-maintained?)

@Kibadda
Copy link
Owner

Kibadda commented Nov 22, 2023

Yeah, when I first started this I was just implementing what I was using. I know that a lot is still missing. :)

The provided query files are not perfect. I was trying to mimic html tag highlighting.

If you find something that you can improve, don't hesitate to open a pr :)

@danjessen
Copy link
Author

danjessen commented Nov 22, 2023

I tried to look at blade and twig and maybe liquid. But sadly I didn't spot a liquid tree-sitter. It's interesting since the syntax is so similar.

Edit: I guess there is a liquid one. But it's missing queries. Which is the interesting bit

@danjessen
Copy link
Author

danjessen commented Nov 24, 2023

I chattede with ChatGPT because I wasn't close to a PC this week. This is just a minor note:

module.exports = grammar({
  name: 'smarty',

  extras: $ => [/\s/],

  rules: {
    // Control Structures
    if_statement: $ => seq(
      '{if',
      $.expression,
      '}',
      $.body,
      optional($.else_clause),
      '{/if}'
    ),
    elseif_clause: $ => seq(
      '{elseif',
      $.expression,
      '}'
    ),
    else_clause: $ => '{else}',

    // Assignments and Variable Usage
    assignment: $ => seq(
      '{assign',
      $.variable,
      $.expression,
      '}'
    ),
    variable: $ => /[a-zA-Z_][a-zA-Z0-9_]*/,
    hash_marked_variable: $ => seq(
      '{#',
      choice('$', ''),
      $.variable,
      repeat(seq('.', $.variable)),
      '#}'
    ),
    variable_usage: $ => choice(
      $.variable,
      $.hash_marked_variable,
      // Add other variations if needed
    ),

    // Function Calls
    function_call: $ => seq(
      '{call',
      $.function_name,
      repeat($.function_argument),
      '}'
    ),
    function_argument: $ => choice(
      $.variable,
      $.string,
      $.number
    ),
    function_name: $ => /[a-zA-Z_][a-zA-Z0-9_]*/,

    // Comments
    comment: $ => seq(
      '{*',
      /[^*]*\*+([^/*][^*]*\*+)*/,
      '*}'
    ),

    // Includes and Extensions
    include: $ => seq(
      '{include',
      $.file_path,
      '}'
    ),
    extends: $ => seq(
      '{extends',
      $.file_path,
      '}'
    ),
    
    // Loops
    foreach_statement: $ => seq(
      '{foreach',
      $.variable,
      'as',
      $.key,
      '=>',
      $.value,
      '}',
      $.body,
      '{/foreach}'
    ),
    for_statement: $ => seq(
      '{for',
      $.init_expression,
      $.condition,
      $.loop_expression,
      '}',
      $.body,
      '{/for}'
    ),

    // File Path
    file_path: $ => /"[^"]*"/,

    // Expressions
    expression: $ => /[^\n{}]+/,
    body: $ => repeat(choice($.expression, $.if_statement, $.comment, $.function_call, $.include, $.extends, $.foreach_statement, $.for_statement)),
  }
});

@Kibadda
Copy link
Owner

Kibadda commented Feb 15, 2024

Hi I don't know if you are still using this.
But just as an FYI:
I refactored the parser in the dev branch.
I added the missing built-in functions, custom functions and modifiers for variables.
Also I updated the query files.

What is still missing:

  • custom user functions/blocks
  • modifiers for function calls

So if you like you can test that out and let me know how you like the changes.

@danjessen
Copy link
Author

I'm using it daily. Ill try out the dev branch during next week

@danjessen
Copy link
Author

Screenshot 2024-02-21 at 08 57 42

@Kibadda
Copy link
Owner

Kibadda commented Feb 22, 2024

How did you add the smarty parser to nvim-treesitter?

@danjessen
Copy link
Author

return {
  "nvim-treesitter/nvim-treesitter",
  build = ":TSUpdate",
  event = { "LazyFile", "VeryLazy" },
  config = function(_, opts)
    if type(opts.ensure_installed) == "table" then
      ---@type table<string, boolean>
      local added = {}
      opts.ensure_installed = vim.tbl_filter(function(lang)
        if added[lang] then
          return false
        end
        added[lang] = true
        return true
      end, opts.ensure_installed)
    end

    require("nvim-treesitter.parsers").get_parser_configs().smarty = {
      install_info = {
        url = "https://github.com/Kibadda/tree-sitter-smarty",
        files = { "src/parser.c" },
        branch = "main",
      },
      filetype = "smarty", -- if filetype does not match the parser name
    }
    require("nvim-treesitter.parsers").get_parser_configs().sql = {
      install_info = {
        url = "https://github.com/DerekStride/tree-sitter-sql",
        files = { "src/parser.c" },
        branch = "main", -- default branch in case of git repo if different from master
        generate_requires_npm = false, -- if stand-alone parser without npm dependencies
        requires_generate_from_grammar = false, -- if folder contains pre-generated src/parser.c
      },
      filetype = "sql", -- if filetype does not match the parser name
    }

    require("nvim-treesitter.configs").setup(opts)
  end,
  opts = function(_, opts)
    vim.list_extend(opts.ensure_installed, {
      "smarty",
      "sql",
      "php",
      "phpdoc",
      "fish",
      "scss",
      "css",
      "toml",
      "git_config",
    })

    opts.highlight = {
      enable = true,
      additional_vim_regex_highlighting = false,
    }
  end,
}

@Kibadda
Copy link
Owner

Kibadda commented Feb 22, 2024

You need to update the parser config as shown in the README.md of the dev branch.

require("nvim-treesitter.parsers").get_parser_configs().smarty = {
  install_info = {
    url = "https://github.com/Kibadda/tree-sitter-smarty",
    files = { "src/parser.c", "src/scanner.c" },
    branch = "dev",
  },
}

@danjessen
Copy link
Author

Sorry, yes. I did that. I copied the wrong code since I reverted back to "master" to avoid getting spammed with errors while working.

@danjessen
Copy link
Author

To clarify ... I used the correct branch and the new queries. But I was still experiencing the error.

@danjessen
Copy link
Author

danjessen commented Mar 5, 2024

You need to update the parser config as shown in the README.md of the dev branch.

require("nvim-treesitter.parsers").get_parser_configs().smarty = {
  install_info = {
    url = "https://github.com/Kibadda/tree-sitter-smarty",
    files = { "src/parser.c", "src/scanner.c" },
    branch = "dev",
  },
}
Screenshot 2024-03-05 at 09 47 49

I'm still getting an error when using your snippet vs my own. Are there any kind of dependencies I need or I it setup with playground something of that kind.

I've even tried resetting nvim completely by moving/ deleting cache and state.

@Kibadda
Copy link
Owner

Kibadda commented Mar 7, 2024

Honestly I have no idea why this does not compile. I just tried with a minimal reproduction on my computer and I could install this parser without any issues.

@Kibadda
Copy link
Owner

Kibadda commented Mar 7, 2024

The only dependencies you need, are the ones listed in the nvim-treesitter repository.

@danjessen
Copy link
Author

danjessen commented Mar 7, 2024

I finally did more of a general system update. Updated tree-sitter (v0.21.0 now) and updated system build tools in general (Sonoma 14.3.1). That seemed to fix the build issue.

With the default queries. I'm not certain the new one is better. I'll have to play more with custom queries or different color schemes.

Screenshot 2024-03-07 at 13 57 41

@Kibadda
Copy link
Owner

Kibadda commented Mar 7, 2024

I can see why. Non builtin functions like {CheckoutPaymentMethods} are not supported.

For reference this is how it looks with my colorscheme

grafik

I tried to mimic the html highlights.

@danjessen
Copy link
Author

danjessen commented Mar 7, 2024

Which if fair. Its a smarty component built with smarty plugins. But there are also smarty functions, that can be called with {call name parameters} or {name parameters}

@gmnz
Copy link

gmnz commented Nov 22, 2024

You need to update the parser config as shown in the README.md of the dev branch.

require("nvim-treesitter.parsers").get_parser_configs().smarty = {
  install_info = {
    url = "https://github.com/Kibadda/tree-sitter-smarty",
    files = { "src/parser.c", "src/scanner.c" },
    branch = "dev",
  },
}

Hi, first of all thanks for the work on this! I'm getting errors with some of the queries from the queries folder. Are they all still valid for the dev branch or has treesitter query syntax changed in the meanwhile or what could be the problem? For example the in highlights the stuff marked as @tag doesn't seem to work for me / gives me errors.

@gmnz
Copy link

gmnz commented Nov 23, 2024

OK, so after playing around with treesitter and smarty I got highlights and injections working pretty well, but I can't get indents to work. Do they work for you two? If yes, then how? 😁

@Kibadda
Copy link
Owner

Kibadda commented Nov 26, 2024

Sorry for the late response. Unfortunately they do not work for me either. To be honest I could not find out why.
Maybe there is something wrong with the queries.

@gmnz
Copy link

gmnz commented Nov 29, 2024

That's a pity. Does indenting work for you, @danjessen ?

@danjessen
Copy link
Author

I took a different approach. I'm using https://github.com/shadowwa/smarty.vim

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

3 participants