Giter Club home page Giter Club logo

Comments (6)

romgrk avatar romgrk commented on May 23, 2024

We should find a way to add that, I'll take a look eventually, PR welcome.

from nvim-treesitter-context.

camgraff avatar camgraff commented on May 23, 2024

adding else to the default patterns accomplishes this.

    patterns = { -- Match patterns for TS nodes. These get wrapped to match at word boundaries.
        -- For all filetypes
        -- Note that setting an entry here replaces all other patterns for this entry.
        -- By setting the 'default' entry below, you can control which nodes you want to
        -- appear in the context window.
        default = {
            'class',
            'function',
            'method',
             'for',
             'while',
             'if',
             'switch',
             'case',
             'else'
        },

Screenshot from 2021-10-31 21-46-34

from nvim-treesitter-context.

ouuan avatar ouuan commented on May 23, 2024

adding else to the default patterns accomplishes this.

This works for Python but does not work for C++, because Python has an else_clause node, but in C++, it's just an expression_statement.

from nvim-treesitter-context.

ouuan avatar ouuan commented on May 23, 2024

C++:

if (1) 1;
else if (2) 2;
else 3;
translation_unit [0, 0] - [3, 0]
  if_statement [0, 0] - [2, 7]
    condition: condition_clause [0, 3] - [0, 6]
      value: number_literal [0, 4] - [0, 5]
    consequence: expression_statement [0, 7] - [0, 9]
      number_literal [0, 7] - [0, 8]
    alternative: if_statement [1, 5] - [2, 7]
      condition: condition_clause [1, 8] - [1, 11]
        value: number_literal [1, 9] - [1, 10]
      consequence: expression_statement [1, 12] - [1, 14]
        number_literal [1, 12] - [1, 13]
      alternative: expression_statement [2, 5] - [2, 7]
        number_literal [2, 5] - [2, 6]

Python:

if 1:
	1
elif 2:
	2
else:
	3
module [0, 0] - [6, 0]
  if_statement [0, 0] - [5, 2]
    condition: integer [0, 3] - [0, 4]
    consequence: block [1, 1] - [1, 2]
      expression_statement [1, 1] - [1, 2]
        integer [1, 1] - [1, 2]
    alternative: elif_clause [2, 0] - [3, 2]
      condition: integer [2, 5] - [2, 6]
      consequence: block [3, 1] - [3, 2]
        expression_statement [3, 1] - [3, 2]
          integer [3, 1] - [3, 2]
    alternative: else_clause [4, 0] - [5, 2]
      body: block [5, 1] - [5, 2]
        expression_statement [5, 1] - [5, 2]
          integer [5, 1] - [5, 2]

JavaScript's if else is similar to C++'s, but there's an else_clause in JavaScript:

if (1) 1;
else if (2) 2;
else 3;
program [0, 0] - [3, 0]
  if_statement [0, 0] - [2, 7]
    condition: parenthesized_expression [0, 3] - [0, 6]
      number [0, 4] - [0, 5]
    consequence: expression_statement [0, 7] - [0, 9]
      number [0, 7] - [0, 8]
    alternative: else_clause [1, 0] - [2, 7]
      if_statement [1, 5] - [2, 7]
        condition: parenthesized_expression [1, 8] - [1, 11]
          number [1, 9] - [1, 10]
        consequence: expression_statement [1, 12] - [1, 14]
          number [1, 12] - [1, 13]
        alternative: else_clause [2, 0] - [2, 7]
          expression_statement [2, 5] - [2, 7]
            number [2, 5] - [2, 6]

from nvim-treesitter-context.

lewis6991 avatar lewis6991 commented on May 23, 2024

Issue is now stale due to #198

from nvim-treesitter-context.

tom-anders avatar tom-anders commented on May 23, 2024

So should this work now for all supported languages now? I checked with C++ and the else still isn't displayed as context

from nvim-treesitter-context.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.