zokugun / vscode-explicit-folding

Customize your Folding for Visual Studio Code
MIT License
99 stars 14 forks source link

Comments in Cobol #47

Closed FALLAI-Denis closed 3 years ago

FALLAI-Denis commented 3 years ago

Describe the issue

I wanted to set up a recognition of blocks of "comment" lines. In COBOL, a comment is introduced indicating the "*" character in column 7 of the source line.

I declare an identical begin expression and end expression, which is recognized as a "docstring" by the extension.

For a group of contiguous lines, instead of having a single fold block for all of the lines, I have a fold every 2 lines.

To reproduce

Code Example

      * comment 1
      * comment 2
      * comment 3
      * comment 4
      * comment 5
      * comment 6
      * comment 7
      * comment 8
      * comment 9
      * comment 10
      * comment 11
      * comment 12

Settings

    "folding":
      {"cobol": [
        {// Bloc commentaires
         "beginRegex":"(?<=^.{6})\\*"
        ,"endRegex":"(?<=^.{6})\\*"
      //  ,"kind": "comment"
        } 
      ]

}

Expected behavior

A single folding for all the contiguous lines.

Screenshots

image

lang: cobol, regex: /(?<_4_0>(?<=^.{6})\*)/g
line: 1, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 2, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 3, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 4, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 5, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 6, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 7, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 8, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 9, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 10, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 11, offset: 0, type: DOCSTRING, match: *, regex: 0
line: 12, offset: 0, type: DOCSTRING, match: *, regex: 0
foldings: [{"start":0,"end":1,"kind":3},{"start":2,"end":3,"kind":3},{"start":4,"end":5,"kind":3},{"start":6,"end":7,"kind":3},{"start":8,"end":9,"kind":3},{"start":10,"end":11,"kind":3}]

Additional context

The problem also seems to affect the operation of "separators": depending on the number of odd or even lines of "docstring", a "separator" type folding disappears before or after the "docstrings" block.

What is the difference between "docstring" and type "comment"? Is a "docstring" automatically a "comment" or must it be specified explicitly?

daiyam commented 3 years ago

There is no possible config to fold single line comments, yet.

daiyam commented 3 years ago

Can you the update explicit-folding-0.13.0.vsix with the following config:

"folding":
      {"cobol": [
        {// Bloc commentaires
         "beginRegex":"(?<=^.{6})\\*"
        ,"endRegex":"(?<=^.{6})[^*]"
        ,"nested": false
        ,"foldLastLine": false
      //  ,"kind": "comment"
        } 
      ]
}
FALLAI-Denis commented 3 years ago

Hi,

This was my first approach to the problem. But it didn't work because the ending regular expression conflicted with the other folding rules. It also generates a lot of "matches" (almost all lines of the source code).

It would seem that the same expression cannot participate in more than one folding rule, and / or be both a begin expression and an end expression in two different folding rules. The order in which the rules are declared also seems to matter: first come, first served.

I also discovered that it was often useless to code lookbehind and lookafter because after all only the line number is important and is used to define the ranges of the foldings areas. A priori this can even harm the performance of the execution of regular expressions without adding more value to the result.

The lookbehind and lookafter are in fact only useful to discriminate some particular use cases.

What is the purpose of the "DOCSTRING" type search if it does not apply to the current use case?

I will refine my tests and get back to you soon.

daiyam commented 3 years ago

Docstrings are comment blocks in python (enclosed in """). Since the begin/end are identical, it has to be managed differently than the regular begin/end. So I've just called that mode DOCSTRING...

FALLAI-Denis commented 3 years ago

New test (limited to comment lines block), with last version 0.13.0

    "folding":
      {"cobol": [
        {// Bloc commentaires
         "beginRegex": "^.{6}\\*"
        ,"endRegex": "^.{6}[^*]"
        //,"kind": "comment"
        } 
      ]
  }

image

lang: cobol, regex: /(?<_0_0>^.{6}\*)|(?<_2_0>^.{6}[^*])/g
line: 1, offset: 0, type: END, match:        , regex: 0
line: 2, offset: 0, type: END, match:        , regex: 0
line: 3, offset: 0, type: BEGIN, match:       *, regex: 0
line: 4, offset: 0, type: BEGIN, match:       *, regex: 0
line: 5, offset: 0, type: BEGIN, match:       *, regex: 0
line: 6, offset: 0, type: BEGIN, match:       *, regex: 0
line: 7, offset: 0, type: BEGIN, match:       *, regex: 0
line: 8, offset: 0, type: BEGIN, match:       *, regex: 0
line: 9, offset: 0, type: BEGIN, match:       *, regex: 0
line: 10, offset: 0, type: BEGIN, match:       *, regex: 0
line: 11, offset: 0, type: BEGIN, match:       *, regex: 0
line: 12, offset: 0, type: BEGIN, match:       *, regex: 0
line: 13, offset: 0, type: BEGIN, match:       *, regex: 0
line: 14, offset: 0, type: BEGIN, match:       *, regex: 0
line: 15, offset: 0, type: END, match:        , regex: 0
line: 16, offset: 0, type: END, match:        , regex: 0
line: 17, offset: 0, type: END, match:        , regex: 0
foldings: [{"start":13,"end":14,"kind":3},{"start":12,"end":15,"kind":3},{"start":11,"end":16,"kind":3}]
FALLAI-Denis commented 3 years ago

PS : works with "nested":false (don't understand why... but global regex not the same, look like "docstring". does endregex matter ?).

    "folding":
      {"cobol": [
        {// Bloc commentaires
         "beginRegex": "^.{6}\\*"
        ,"endRegex": "^.{6}[^*]"
        ,"nested": false
        //,"kind": "comment"
        } 
      ]
  }

image

image

lang: cobol, regex: /(?<_0_0>^.{6}\*)/g
line: 3, offset: 0, type: BEGIN, match:       *, regex: 0
[nested] line: 15, offset: 0, type: END, match:        
foldings: [{"start":2,"end":14,"kind":3}]
daiyam commented 3 years ago

"nested": false indicates there is no nested foldings inside the current folding. So when the beginRegex is matched, it goes to another loop to just match the endRegex. You should use "foldLastLine": false to not include the first line which isn't a comment (PROCEDURE DIVISION).

FALLAI-Denis commented 3 years ago

Works fine.

    "folding":
      {"cobol": [
        {// Bloc commentaires
         "beginRegex": "^.{6}\\*"
        ,"endRegex": "^.{6}[^*]"
        ,"nested": false
        ,"foldLastLine": false
        ,"kind": "comment"
        } 
    ]
}

"Shift+Alt+A" ("Maj+Alt+A" with French Language Pack) has no effect on block comment lines... even with "kind": "comment".

daiyam commented 3 years ago

I've published the updated extension. I've also improved the debug messages.

daiyam commented 3 years ago

@FALLAI-Denis Thank you so much for your kind donation. You made my day! :smile:

FALLAI-Denis commented 3 years ago

This issue is resolved.