Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Line Comment-Action #42

Open
goulashsoup opened this issue Mar 16, 2020 · 13 comments
Open

Add Line Comment-Action #42

goulashsoup opened this issue Mar 16, 2020 · 13 comments

Comments

@goulashsoup
Copy link

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

@SimonIT
Copy link
Collaborator

SimonIT commented Mar 18, 2020

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

@goulashsoup
Copy link
Author

goulashsoup commented Mar 30, 2020

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?

@SimonIT
Copy link
Collaborator

SimonIT commented Mar 30, 2020

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

@goulashsoup
Copy link
Author

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.

@WebMechanic
Copy link

WebMechanic commented Apr 3, 2020

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

@SimonIT
Copy link
Collaborator

SimonIT commented Apr 3, 2020

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

@WebMechanic
Copy link

WebMechanic commented Apr 3, 2020

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

@takanuva15
Copy link

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");
    }
}

@iamqiz
Copy link
Contributor

iamqiz commented Jul 28, 2022

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

@smaudet
Copy link

smaudet commented Nov 14, 2022

@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.

@takanuva15
Copy link

@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)

@smaudet
Copy link

smaudet commented Apr 22, 2023

@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...

@takanuva15
Copy link

@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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants