atom / language-c

C support in Atom
Other
118 stars 152 forks source link

c++ highlighting and code folding gets confused with structural binding in fancy for loop #325

Closed AminGholizad closed 5 years ago

AminGholizad commented 5 years ago

Description

for (auto [x,y]:vec) makes the highlighting and code folding behave strangely. it gets confused about which { belongs to which } and code inside the for loop gets confused.

image

workaround that I use

image

Versions

atom --version: Atom : 1.36.0 Electron: 2.0.18 Chrome : 61.0.3163.100 Node : 8.9.3 apm --version: apm 2.1.3 npm 6.2.0 node 8.9.3 x64 atom 1.36.0 python 3.7.0 git visual studio

OS: windows 10


Edit: Look at the difference in the int and if between the two images.

50Wliu commented 5 years ago

Thanks for creating the issue; confirmed.

Ben3eeE commented 5 years ago

This is because of a parsing error in tree-sitter:

int main () {
  for (auto [x,y]:vec) {
    int z = 0;
    if (x >0) {

    }
  }
}
translation_unit [0, 0] - [9, 0])
  function_definition [0, 0] - [7, 1])
    primitive_type [0, 0] - [0, 3])
    function_declarator [0, 4] - [0, 11])
      identifier [0, 4] - [0, 8])
      parameter_list [0, 9] - [0, 11])
    compound_statement [0, 12] - [7, 1])
      ERROR [1, 2] - [6, 3])
        ERROR [1, 7] - [1, 18])
          auto [1, 7] - [1, 11])
          lambda_capture_specifier [1, 12] - [1, 17])
            identifier [1, 13] - [1, 14])
            identifier [1, 15] - [1, 16])
        identifier [1, 18] - [1, 21])
        ERROR [1, 21] - [2, 13])
          assignment_expression [2, 4] - [2, 13])
            identifier [2, 4] - [2, 7])
            ERROR [2, 8] - [2, 9])
              identifier [2, 8] - [2, 9])
            number_literal [2, 12] - [2, 13])
        call_expression [3, 4] - [3, 13])
          identifier [3, 4] - [3, 6])
          argument_list [3, 7] - [3, 13])
            relational_expression [3, 8] - [3, 12])
              identifier [3, 8] - [3, 9])
              number_literal [3, 11] - [3, 12])

/cc: @maxbrunsfeld I couldn't find a reference for the valid syntax for this. It seems like tree-sitter-cpp parses some range based for loops but not if it has structural binding.

maxbrunsfeld commented 5 years ago

Thanks for the report! This will be fixed in Atom 1.38 (and 1.38-beta0).