1

I'm writing a VSCode plugin to handle a certain scripting language.

This is the "language-configuration.json" file:

{
  "comments": {
    "lineComment": {
      "comment": "//",
      "noIndent": true
    }
  },
  "brackets": [
    ["(", ")"],
    ["[", "]"]
  ],
  "autoClosingPairs": [
    ["(", ")"],
    ["[", "]"],
    { "open": "'", "close": "'", "notIn": ["string", "comment"] },
    { "open": "\"", "close": "\"", "notIn": ["string"] },
    { "open": "`", "close": "`", "notIn": ["string", "comment"] }
  ],
  "surroundingPairs": [
    ["(", ")"],
    ["'", "'"],
    ["\"", "\""],
    ["[", "]"],
    ["%", "%"],
    ["<", ">"]
  ],
  "colorizedBracketPairs": [
    ["(", ")"],
    ["[", "]"]
  ],
  "autoCloseBefore": ";:.,=}])>` \n\t",
  "folding": {
    "markers": {
      "start": "^//section>.*$",
      "end": "^//endsection>.*$"
    }
  },
  "indentationRules": {
    "increaseIndentPattern": "^((?!\\/\\/).)*(\\([^)\"'`]*|\\[[^\\]\"'`]*)$",
    "decreaseIndentPattern": "^((?!\\/\\/).)*\\s*[\\)\\]].*$"
  },
  "onEnterRules": [
    {
      "beforeText": "^((?!\\/\\/).)*\\s*\\$[a-zA-Z0-9_]+\\([a-zA-Z0-9%_]+,",
      "action": { "indent": "indent" }
    },
    {
      "beforeText": "^((?!\\/\\/).)*\\s*.*,",
      "action": { "indent": "none" }
    },
    {
      "beforeText": "^\\/\\/$",
      "action": {
        "indent": "none"
      }
    },
    {
      "beforeText": "^\\/\\/\\s+.*$",
      "action": {
        "indent": "none",
        "appendText": "// "
      }
    }
  ]
}

The indentationRules and the onEnterRules are tested on the following code:

$trim()
$trim(%paster%,kalosh)

The lines of code are numbered from 1 to 2. I get the following results (in the following code, the | symbol indicates the cursor position)

line 1

-1- before pressing "enter"

$trim(|)

after pressing "enter"

$trim(
  |
)

This is the expected result.

-2- before pressing "enter"

$trim()|

after pressing "enter"

$trim()
|

This is the expected result.

line 2

-1- before pressing "enter"

$trim(|%paster%,kalosh)

after after pressing "enter"

$trim(
  |%paster%,kalosh)

This is the expected result.

-2- before pressing "enter"

$trim(%paster%,|kalosh)

after pressing "enter"

$trim(%paster%,
  |kalosh)

This is the expected result.

-3- before pressing "enter"

$trim(%paster%,kalosh|)

after pressing "enter"

$trim(%paster%,kalosh
  |)

NOT THE EXPECTED RESULT. The new line should have the same indentation as the previous one.

-4- before pressing "enter"

$trim(%paster%,kalosh)|

after pressing "enter"

$trim(%paster%,kalosh)
  |

NOT THE EXPECTED RESULT. The new line should have the same indentation as the previous one. I notice that in the first line of code, the same function but without parameters, the behavior is correct.

Question

How can I keep all the rules intact and also add the rule that says "if the cursor is at the end of a uncommented line, the next line should retain the indentation level of the current one"?

To try to solve the problem, I tried inserting the following as the first rule in onEnterRules:

{
  "beforeText": "^((?!\\/\\/).)*",
  "afterText": "\\s*$",
  "action": { "indent": "none" }
}

But not only did I not get the result I was looking for, but all the other rules were also not triggered.

1 Answer 1

1

Solved.

I submitted the problem to Copilot, Gemini, and Stack Overflow AI, and none of them were able to find the problem and fix it.

All I had to do was change the first two rules in onEnterRules to this:

...
{
  "beforeText": "^((?!\\/\\/).)*\\s*\\$[a-zA-Z0-9_]+\\([a-zA-Z0-9%_]+,$",
  "action": { "indent": "indent" }
},
{
  "beforeText": "^((?!\\/\\/).)*\\s*.*,$",
  "action": { "indent": "none" }
},
...

Basically, I added the end-of-line character $ to beforeText, and everything started working as expected again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.