Comments (13)
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");
}
}
from idea-batch.
You can use the normal commenting shortcut Strg+/ or over the context menu (theoretically). Or what do you mean by command?
from idea-batch.
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 batch file missing:
Or maybe there is something i dont know, i checked if the plugin is installed. Is there some configuration missing?
from idea-batch.
The thing is, that these options exists if I run the plugin via gradle in intellij:
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
from idea-batch.
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.
from idea-batch.
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
from idea-batch.
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
from idea-batch.
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
from idea-batch.
Ctrl+/ line comment doesn't work for me in GoLand 2022.1.4 😣
from idea-batch.
@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.
from idea-batch.
@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)
from idea-batch.
@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...
from idea-batch.
@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?
from idea-batch.
Related Issues (20)
- Colours inherit from wrong language defaults
- IDEA 2019.2: java.lang.ClassNotFoundException: sun.reflect.Reflection PluginClassLoader HOT 19
- Plugin is not compatible with IntelliJ 2019.3 HOT 2
- The plugin is not compatible with IDEA 2019.1 HOT 1
- Red underlines after `if defined` HOT 1
- Cannot find stuff through Conda environment HOT 1
- Doesn't work in 2018.3.6 HOT 1
- typing :: breaks the editor HOT 9
- Move command dont works HOT 2
- "Script" input shouldn't replace "\" with "/" character HOT 4
- When using psql command, input prompt is not shown, and input does not work
- How to pass $ContentRoot$ or $ModuleName$ to .bat HOT 1
- make "control+/" do block comments like in python HOT 1
- Is there a way to enable ansi colors in output?
- Remove Spaces from Plugin id HOT 4
- Consistent icon
- Error at startup with Intellij 2021.3 EAP HOT 7
- Chinese garbled code when run a bat file on window 10
- %ERRORLEVEL% doesn't color correctly / isn't recognized
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from idea-batch.