I just filed https://github.com/BetterThanTomorrow/calva/issues/2872, which has been an issue that's been bugging me for a while. I'm happy to take a shot at a PR if it's something straightforward and you can point me in the right direction. If not, let me know if there's anything I can do to assist!
Thanks! I commented on the issue.
I gather from the f1 menu's gear button that the command is editor.action.addCommentLine, which is to say, not part of Calva?
It’s a VS Code built-in command.
@pez thoughts on this?
It’s quite strange why Calva makes a difference. I don’t really have a clue. But it won’t matter this time because @jonurnieta is introducing a structural toggle-comment command: https://github.com/BetterThanTomorrow/calva/pull/3047
I know this thread has been on ice for a long time, but this issue has been annoying me again so I'm making an attempt to revive it. For those just tuning in, here's the issue I want to fix. Say you have this form (where | is the cursor):
(assoc {}
|
:foo
1)
If you trigger editor.action.commentLine, you'd like to get this:
(assoc {}
;; |
:foo
1)
What you get instead is this:
(assoc {}
;; |
:foo
1)
I've previously determined that this behavior is definitely coming from vscode, not calva. But I discovered today that calva does produce the correct behavior in this scenario:
(defn a []
|
b)
And without calva, it does this instead:
(defn a []
;; |
b)
That means Calva's definitely doing something in the defn scenario, and could be changed to do something in the assoc scenario. Any ideas on where in Calva's code it's overriding vscode's default behavior in the defn case?I’m thinking that a way to deal with it is to add a command to calva for commenting lines and then users can override the default shortcut to use Calva’s command instead. Similar to how you can bind ; to our custom command for inserting ;, I have this in my keybindings.json:
{
"command": "paredit.insertSemiColon",
"key": ";",
"when": "calva:keybindingsEnabled && editorLangId == clojure && editorTextFocus && paredit:keyMap == strict && !editorReadOnly && !editorHasMultipleSelections && !calva:cursorInComment"
},what do you think about calling getIndent for the commented lines? it could be a calva command that runs with same shortcut as editor.action.commentLine
Another option is , without Calva command, adding a fn to fix commented line indents after comment toggle is done
If we can detect when comment toggle is done, that would be cleanest. But only if there is a clean way to detect this. Which I doubt a bit that there is. So separate calva command for toggling comments on a line is the way to go, I think. And this command should use the existing insertSemoColon strategy so that the toggle is structural.
The fact that we want the toggle to be structural is, alone, pointing us towards a custom command, I think.
for detecting when toggle comment is done, I think there is not direct way of doing that, it could be done on edit checking if line starts with ";"
otherwise a calva command that acts like editor.action.commentLine + indents could solve it
> a calva command that acts like editor.action.commentLine + indents could solve it
Like a structural editor.action.commentLine , right? 😃
It would be extremely messy to check if edits starts with ; and decide if this is because of toggle.
I'm still curious about what's causing the correct indenting in the defn case. It must be something in calva because when calva is disabled, it indents wrong. Any ideas?
Fwiw if I hit tab it moves the ;; to the correct indentation, so calva’s formatter does work correctly when prompted to do so. You still end up with extra spaces after the ;; though
I think it could be Calva’s language configurations that make defn work. It is the default 2-space indent that we would get if we didn’t also do dynamic indentation.
I'll look into that, I was looking at calva’s language definition and the default clojure one side by side and nothing initially jumped out at me
Yeah, didn’t remember about the default Clojure one, tbh. But Calva has a dynamic language definition provider. Was that the one you looked at?
I don’t recall how dynamic. Just that it isn’t in json, but in typescript.
No, I was looking at the static one that’s checked into calva. Where would I go looking for the dynamic one?
Maybe I am misleading you with calling it language definition. I mean this thing in extension.ts:
function getLanguageConfiguration(): vscode.LanguageConfiguration {
const languageConfiguration = {
onEnterRules: isOldIndentEngineInPlay()
? [
// When cljfmt is used for indenting, disable all vscode default indentation
// (By outdenting a lot, which is the only way I have found that works)
{
beforeText: /.*/,
action: {
indentAction: vscode.IndentAction.Outdent,
removeText: Number.MAX_VALUE,
},
},
]
: [],
};
console.log('Issue #2071: languageConfiguration', languageConfiguration);
console.log('Issue #2071: formatOnType?', config.formatOnTypeEnabled());
console.log(
'Issue #2071: newIndentEngine?',
vscode.workspace.getConfiguration('calva.fmt').get('newIndentEngine')
);
return languageConfiguration;
}As far as I can tell, the language configuration calva is setting is just this: {onEnterRules: []}. I'm not sure how that would result in the behavior we're seeing? That must be merged with some kind of existing language configuration, like the one in calva's static language-configuration.json, right?
It appears that the only difference between calva's language-configuration.json and the default vscode one is that calva's sets wordPattern