Search in sources :

Example 6 with CommandLineFile

use of com.jetbrains.commandInterface.commandLine.psi.CommandLineFile in project intellij-community by JetBrains.

the class CommandModeConsumer method consume.

@Override
public void consume(final String t) {
    /**
     * We need to: 1) parse input 2) fetch command 3) split its arguments.
     */
    final PsiFileFactory fileFactory = PsiFileFactory.getInstance(myModule.getProject());
    final CommandLineFile file = PyUtil.as(fileFactory.createFileFromText(CommandLineLanguage.INSTANCE, t), CommandLineFile.class);
    if (file == null) {
        return;
    }
    final String commandName = file.getCommand();
    final List<String> commandAndArgs = Arrays.asList(EMPTY_SPACE.split(file.getText().trim()));
    // 1 because we need to remove command which is on the first place
    final List<String> args = (commandAndArgs.size() > 1 ? commandAndArgs.subList(1, commandAndArgs.size()) : Collections.<String>emptyList());
    for (final Command command : myCommands) {
        if (command.getName().equals(commandName)) {
            command.execute(commandName, myModule, args, myConsole);
            return;
        }
    }
    if (myDefaultExecutor != null && !commandAndArgs.isEmpty()) {
        // Unknown command execution is delegated to default executor
        myDefaultExecutor.execute(commandAndArgs.get(0), myModule, args, myConsole);
    } else {
        myConsole.print(PyBundle.message("commandLine.commandNotFound", commandName), ConsoleViewContentType.ERROR_OUTPUT);
        myConsole.print("", ConsoleViewContentType.SYSTEM_OUTPUT);
    }
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) Command(com.jetbrains.commandInterface.command.Command) CommandLineFile(com.jetbrains.commandInterface.commandLine.psi.CommandLineFile)

Example 7 with CommandLineFile

use of com.jetbrains.commandInterface.commandLine.psi.CommandLineFile in project intellij-community by JetBrains.

the class CommandLineCommandReference method getVariants.

@NotNull
@Override
public Object[] getVariants() {
    final CommandLineFile file = getCommandLineFile();
    if (file == null) {
        return EMPTY_ARRAY;
    }
    final List<Command> commands = file.getCommands();
    if (commands == null) {
        return EMPTY_ARRAY;
    }
    final LookupWithIndentsBuilder result = new LookupWithIndentsBuilder();
    for (final Command command : commands) {
        final LookupElementBuilder lookupElementBuilder = LookupElementBuilder.create(command.getName());
        final Help help = command.getHelp(true);
        result.addElement(lookupElementBuilder, (help != null ? help.getHelpString() : null));
    }
    return result.getResult();
}
Also used : Help(com.jetbrains.commandInterface.command.Help) Command(com.jetbrains.commandInterface.command.Command) CommandLineCommand(com.jetbrains.commandInterface.commandLine.psi.CommandLineCommand) CommandLineFile(com.jetbrains.commandInterface.commandLine.psi.CommandLineFile) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) NotNull(org.jetbrains.annotations.NotNull)

Example 8 with CommandLineFile

use of com.jetbrains.commandInterface.commandLine.psi.CommandLineFile in project intellij-community by JetBrains.

the class CommandLineArgsTest method testNoMoreArgs.

/**
   * Ensures argument is not returned if not required
   */
public void testNoMoreArgs() throws Exception {
    CommandTestTools.initFileType();
    final CommandLineFile file = CommandTestTools.createFileByText(myFixture, "command foo bar spam eggs");
    final ValidationResult validationResult = file.getValidationResult();
    assert validationResult != null : "validation failed";
    Assert.assertNull("Argument returned while should not", validationResult.getNextArg());
}
Also used : CommandLineFile(com.jetbrains.commandInterface.commandLine.psi.CommandLineFile)

Example 9 with CommandLineFile

use of com.jetbrains.commandInterface.commandLine.psi.CommandLineFile in project intellij-community by JetBrains.

the class CommandLineInspectionTest method doTest.

/**
   * Enables inspection on testName.cmdline and checks it.
   */
private void doTest() {
    final PsiFile file = myFixture.configureByFile(getTestName(true) + '.' + CommandLineFileType.EXTENSION);
    Assert.assertSame("Bad file type!", CommandLineFile.class, file.getClass());
    final CommandLineFile commandLineFile = (CommandLineFile) file;
    commandLineFile.setCommands(CommandTestTools.createCommands());
    myFixture.enableInspections(CommandLineInspection.class);
    myFixture.checkHighlighting();
}
Also used : CommandLineFile(com.jetbrains.commandInterface.commandLine.psi.CommandLineFile) PsiFile(com.intellij.psi.PsiFile)

Example 10 with CommandLineFile

use of com.jetbrains.commandInterface.commandLine.psi.CommandLineFile in project intellij-community by JetBrains.

the class CommandTestTools method createFileByText.

/**
   * Creates command file by text and  {@link #createCommands() fills it with commands}
   * @param testFixture fixture
   * @param text command text
   * @return command file
   * @see #createCommands()
   */
@NotNull
static CommandLineFile createFileByText(@NotNull final CodeInsightTestFixture testFixture, @NotNull final String text) {
    final CommandLineFile file = (CommandLineFile) testFixture.configureByText(CommandLineFileType.INSTANCE, text);
    file.setCommands(createCommands());
    return file;
}
Also used : CommandLineFile(com.jetbrains.commandInterface.commandLine.psi.CommandLineFile) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

CommandLineFile (com.jetbrains.commandInterface.commandLine.psi.CommandLineFile)10 PsiFile (com.intellij.psi.PsiFile)3 Command (com.jetbrains.commandInterface.command.Command)2 ValidationResult (com.jetbrains.commandInterface.commandLine.ValidationResult)2 NotNull (org.jetbrains.annotations.NotNull)2 LookupElementBuilder (com.intellij.codeInsight.lookup.LookupElementBuilder)1 PsiFileFactory (com.intellij.psi.PsiFileFactory)1 MessageBusConnection (com.intellij.util.messages.MessageBusConnection)1 Argument (com.jetbrains.commandInterface.command.Argument)1 Help (com.jetbrains.commandInterface.command.Help)1 CommandLineCommand (com.jetbrains.commandInterface.commandLine.psi.CommandLineCommand)1 Nullable (org.jetbrains.annotations.Nullable)1