idea-batch icon indicating copy to clipboard operation
idea-batch copied to clipboard

Add `Line Comment`-Action

Open goulashsoup opened this issue 4 years ago • 14 comments

Would be nice to have a REM-Line Comment-Command.

goulashsoup avatar Mar 16 '20 16:03 goulashsoup

You can use the normal commenting shortcut Strg+/ or over the context menu (theoretically). Or what do you mean by command?

SimonIT avatar Mar 18 '20 09:03 SimonIT

So you can comment with your set "Comment" shortcut?

As far as i know, commenting is just an action (i used "Command" in the previous post) which gets a shortcut assigned to it, therefor shortcuts can be changed in PhpStorm. Single line comments are done with the "Comment with Line Comment" action and this action is not available when have batch script opened in a tab.

Comment Action in Php: comment in php file

Comment Action in batch file missing: comment action in batch

Or maybe there is something i dont know, i checked if the plugin is installed. Is there some configuration missing?

goulashsoup avatar Mar 30 '20 08:03 goulashsoup

The thing is, that these options exists if I run the plugin via gradle in intellij: image image But if I press one of them, the cursor goes simply in a new line and doesn't comment.

But I got the plugin installed in my normal IntelliJ, but there isn't the comment option available, too. It's crazy and I don't know what's wrong

SimonIT avatar Mar 30 '20 10:03 SimonIT

The thing is, that these options exists if I run the plugin via gradle in intellij

Maybe another plugin that does not differentiate between batch and another file type...

But I got the plugin installed in my normal IntelliJ, but there isn't the comment option available, too.

Probably because these Actions have to be added when creating a plugin.

goulashsoup avatar Mar 31 '20 08:03 goulashsoup

Ctrl+/ doesn't work for me either, skips to next line (PhpStorm 2019.3), and typing :: breaks the editor and I need to reopen the file. Ctrl+/ would be nice though

WebMechanic avatar Apr 03 '20 08:04 WebMechanic

As I said, wibotwi implemented the commenter for this plugin (this file) and after looking at the example from jetbrains I don't see any differences. However it doesn't work like you said, but I can't find the problem

SimonIT avatar Apr 03 '20 10:04 SimonIT

I too have these menu items, but they don't do anything -- or actually behave like Ctrl+/ and just move the cursor to the next line. So basically same as here

WebMechanic avatar Apr 03 '20 10:04 WebMechanic

Hi all, I know it's been 2 years since this issue was raised but just wanted to give an update. I have some experience with IntelliJ language plugin development and I did some debugging today on this particular issue. It turns out that this plugin does not define a PsiFile type for Batch scripts so when IntelliJ executes the "Toggle Line Comment" action, it "calculates" that the PsiFile type for the .bat file must be "plain text" and so it essentially does nothing except move the cursor down when you execute the line comment action.

To verify this, you can set a debug point at the return line in this method in CommentByLineCommentHandler.java:

@Nullable
private static Commenter findCommenter(@NotNull Editor editor, @NotNull PsiFile file, final int line) {
  final FileType fileType = file.getFileType();
  if (fileType instanceof AbstractFileType) {
    return ((AbstractFileType)fileType).getCommenter();
  }
  final Language lineStartLanguage = getLineStartLanguage(editor, file, line);
  final Language lineEndLanguage = getLineEndLanguage(file, editor, line);
  return CommentByBlockCommentHandler.getCommenter(file, editor, lineStartLanguage, lineEndLanguage);
}

and you will see that lineStartLanguage and lineEndLanguage are both registered as "plain text" instead of "Batch".

You can see where the PsiFile gets detected from by setting a debug point on this line:

private static void iterateOverCarets(@NotNull final Project project,
                               @NotNull final Editor hostEditor,
                               @NotNull final MultiCaretCodeInsightActionHandler handler) {
  PsiFile hostFile = PsiDocumentManager.getInstance(project).getPsiFile(hostEditor.getDocument());
  ...

To fix this, you would need to follow the plugin documentation starting from this section to register a new Batch PsiFile: https://plugins.jetbrains.com/docs/intellij/lexer-and-parser-definition.html#define-a-root-file Eg:

public class BatchFile extends PsiFileBase {
	...

etc. I attempted to take a stab at it but it's more involved than I initially thought since you would also need a Parser class as defined in the previous section of the documentation here: https://plugins.jetbrains.com/docs/intellij/grammar-and-parser.html

Tbh though, I'm not sure if it's worth it - I had installed this plugin for syntax highlighting but I realized during my testing that IntelliJ by default automatically highlights batch & cmd files, and it also has advanced capabilities around commenting and auto-completing commands. The syntax highlighting in this plugin is prettier than theirs, but when the IntelliJ team themselves are already providing highlighting support for batch files (and will likely continue to update it in the future), then it doesn't really make sense to duplicate their work. I think a better route would be to contact the IntelliJ team on their slack (https://jetbrains-platform.slack.com/) and see if we can configure this plugin to augment the native functionality they are already providing since they do not provide batch run configs or batch file icons. (I am very aware that writing a working Flex file for a language takes many days of testing, so I do understand if the plugin author isn't willing to replace it in favor of IntelliJ's native highlighting)

Last but not least, if a junit test had been added for the commenting functionality before it was merged to master, it would have caught this issue immediately. You can add this Junit test (copied straight from the plugin docs) into this repo and you will see that it fails due to the aforementioned reasons above:

package org.intellij.lang.batch.editor;

import com.intellij.codeInsight.generation.actions.CommentByLineCommentAction;
import com.intellij.testFramework.fixtures.BasePlatformTestCase;
import org.intellij.lang.batch.fileTypes.BatchFileType;
import org.junit.Test;

public class BatchCommenterTest extends BasePlatformTestCase {

    @Test
    public void testCommenter() {
        myFixture.configureByText(BatchFileType.BATCH_FILE_TYPE, "<caret>echo hi");
        CommentByLineCommentAction commentAction = new CommentByLineCommentAction();
        commentAction.actionPerformedImpl(getProject(), myFixture.getEditor());
        myFixture.checkResult("rem echo hi");
        commentAction.actionPerformedImpl(getProject(), myFixture.getEditor());
        myFixture.checkResult("echo hi");
    }
}

takanuva15 avatar Feb 13 '22 18:02 takanuva15

Ctrl+/ line comment doesn't work for me in GoLand 2022.1.4 😣

iamqiz avatar Jul 28 '22 06:07 iamqiz

@takanuva15 is there no way to listen to the keybinding and just insert a rem on the currently selected line(s)?

This should not be that hard, I get the "proper" way of doing it is complex but having to implement a full language file seems overkill.

smaudet avatar Nov 14 '22 15:11 smaudet

@smaudet I don't have hands-on experience hard-wiring a keybinding like that, but I think it could be possible if you were to follow the steps here from the IntelliJ SDK docs and assign a keyboard shortcut to Ctrl + / using the <keyboard-shortcut .../> xml tag. (As an aside, this may be brittle in the case someone wanted to reassign the shortcut to a different hotkey - I'm not sure if IntelliJ would support changing the hotkey for a new custom action natively. Would be easy to test though lol)

In terms of defining a language file, IntelliJ has an opinionated framework for how to implement custom languages. If a developer follows that framework correctly, they're able to tap into the full power of the IntelliJ language engine to do things like commenting, autocomplete, error-checking, etc. with only a few lines of code. This avoids the need for any manual "hacks" that could take longer than just using the framework's natural injection points. (Note: this is not a refutation of anything you said, this is just a point to make based on my experience with developing a custom language plugin)

takanuva15 avatar Jan 01 '23 22:01 takanuva15

@takanuva15 fair enough, perhaps the correct approach then would be to remove any language parsing from this plugin and just have one that does run configurations?

I don't touch batch files often, but its a common enough thing to want to run a script/batch file and be able to both run and edit the code. I am/was happy with IntelliJ's default syntax/comment support, only thing missing was run config...

smaudet avatar Apr 22 '23 18:04 smaudet

@smaudet Yes that should be possible. However, there is also a run configuration type called "Shell Script" within IntelliJ - have you tried using that to configure/run your batch scripts?

takanuva15 avatar Apr 23 '23 14:04 takanuva15