use of com.jetbrains.commandInterface.command.Argument in project intellij-community by JetBrains.
the class CommandLineArgsTest method validateNextArgument.
/**
* Runs commands and checks argument is required after it
* @param commandText command text to run
* @param expectedArgumentText expected next argument help text
*/
private void validateNextArgument(@NotNull final String commandText, @NotNull final String expectedArgumentText) {
final CommandLineFile file = CommandTestTools.createFileByText(myFixture, commandText);
final ValidationResult validationResult = file.getValidationResult();
assert validationResult != null : "validation failed";
final Pair<Boolean, Argument> arg = validationResult.getNextArg();
Assert.assertNotNull("No argument returned, but should", arg);
Assert.assertTrue("Required argument is not marked as required", arg.first);
Assert.assertEquals("Wrong argument text", expectedArgumentText, arg.second.getHelp().getHelpString());
}
use of com.jetbrains.commandInterface.command.Argument in project intellij-community by JetBrains.
the class ValidationResultImpl method visitOption.
@Override
public void visitOption(@NotNull final CommandLineOption o) {
super.visitOption(o);
if (myUnusedOptions.containsKey(o.getOptionName())) {
// Remove from list of available options
final Option option = myUnusedOptions.remove(o.getOptionName());
for (final String optionName : option.getAllNames()) {
myUnusedOptions.remove(optionName);
}
final Pair<Integer, Argument> argumentAndQuantity = option.getArgumentAndQuantity();
if (argumentAndQuantity != null) {
myCurrentOptionAndArgsLeft = Pair.create(option, argumentAndQuantity.first);
} else {
myCurrentOptionAndArgsLeft = new Pair<>(option, 0);
}
} else {
//No such option available
myBadValues.add(o);
}
}
use of com.jetbrains.commandInterface.command.Argument in project intellij-community by JetBrains.
the class CommandLinePsiImplUtils method findBestHelp.
/**
* Tries to find appropriate help for argument. It can be argument help for positional argument or option help
* for option argument.
*
* @param argument argument to search help for
* @return help for argument or null if not found
*/
@Nullable
static Help findBestHelp(@NotNull final CommandLineArgument argument) {
final Option option = argument.findOptionForOptionArgument();
if (option != null) {
return option.getHelp();
}
final Argument realArgument = argument.findRealArgument();
return (realArgument != null ? realArgument.getHelp() : null);
}
use of com.jetbrains.commandInterface.command.Argument in project intellij-community by JetBrains.
the class ValidationResultImpl method processOptionArgument.
private void processOptionArgument(@NotNull final CommandLineArgument o) {
assert myCurrentOptionAndArgsLeft != null : "Method can't be called if no current option exist";
if (myCurrentOptionAndArgsLeft.second > 0) {
myCurrentOptionAndArgsLeft = Pair.create(myCurrentOptionAndArgsLeft.first, myCurrentOptionAndArgsLeft.second - 1);
final Pair<Integer, Argument> argumentAndQuantity = myCurrentOptionAndArgsLeft.first.getArgumentAndQuantity();
// TODO: Use class instead of pair to prevent such a stupid checks
assert argumentAndQuantity != null : "Option has arguments left but no argument info";
final Argument argumentInfo = argumentAndQuantity.getSecond();
processArgument(o, argumentInfo);
myOptionArguments.put(o, myCurrentOptionAndArgsLeft.first);
} else if (myCurrentOptionAndArgsLeft.second == 0) {
myCurrentOptionAndArgsLeft = null;
myExcessArguments.add(o);
}
}
use of com.jetbrains.commandInterface.command.Argument in project intellij-community by JetBrains.
the class ArgumentHintLayer method paintComponent.
@Override
protected void paintComponent(final Graphics g) {
super.paintComponent(g);
final Pair<Boolean, Argument> nextArg = myNextArg;
if (nextArg == null) {
// Nothing to show
return;
}
/**
*
* We should display argument right after trimmed (spaces removed) document end or cursor position what ever comes first.
*
* We also need to add prompt length.
*/
final EditorImpl consoleEditor = PyUtil.as(myConsole.getConsoleEditor(), EditorImpl.class);
if (consoleEditor == null) {
/**
* We can't calculate anything if editor is not {@link EditorImpl})
*/
Logger.getInstance(ArgumentHintLayer.class).warn("Bad editor: " + myConsole.getConsoleEditor());
return;
}
final int consoleFontType = consoleEditor.getCaretModel().getTextAttributes().getFontType();
final FontMetrics consoleFontMetrics = consoleEditor.getFontMetrics(consoleFontType);
final Font consoleFont = consoleFontMetrics.getFont();
// Copy rendering hints
final Graphics2D sourceGraphics2 = PyUtil.as(consoleEditor.getComponent().getGraphics(), Graphics2D.class);
if (sourceGraphics2 != null && g instanceof Graphics2D) {
((Graphics2D) g).setRenderingHints(sourceGraphics2.getRenderingHints());
}
final boolean argumentRequired = nextArg.first;
final String argumentText = nextArg.second.getHelp().getHelpString();
g.setFont(consoleFont);
g.setColor(argumentRequired ? myRequiredColor : myOptionalColor);
final String textToShow = wrapBracesIfNeeded(argumentRequired, StringUtil.isEmpty(argumentText) ? PyBundle.message("commandLine.argumentHint.defaultName") : argumentText);
// Update caret position (if known)
final CaretRectangle[] locations = consoleEditor.getCaretLocations(true);
if (locations != null) {
final CaretRectangle rectangle = locations[0];
myCaretPositionPx = rectangle.myPoint.x;
}
final int consoleEditorTop = consoleEditor.getComponent().getLocation().y;
final double textHeight = Math.floor(consoleFont.getStringBounds(textToShow, consoleFontMetrics.getFontRenderContext()).getY());
// pixels in position should be integer, anyway
@SuppressWarnings("NumericCastThatLosesPrecision") final int y = (int) (consoleEditorTop - textHeight);
// We should take scrolling into account to prevent argument "flying" over text when user scrolls it, like "position:fixed" in css
final Point scrollLocation = consoleEditor.getContentComponent().getLocation();
final int spaceWidth = EditorUtil.getSpaceWidth(consoleFontType, consoleEditor);
// Remove whitespaces on the end of document
/**
* TODO: This is actually copy/paste with {@link com.intellij.openapi.editor.actions.EditorActionUtil#moveCaretToLineEnd}.
* Need to merge somehow.
*/
final String trimmedDocument = StringUtil.trimTrailing(consoleEditor.getDocument().getText());
final double trimmedDocumentWidth = consoleFont.getStringBounds(trimmedDocument, consoleFontMetrics.getFontRenderContext()).getWidth();
// pixels in position should be integer, anyway
@SuppressWarnings("NumericCastThatLosesPrecision") final int contentWidth = (int) Math.ceil(trimmedDocumentWidth + consoleEditor.getPrefixTextWidthInPixels());
g.drawString(textToShow, Math.max(myCaretPositionPx, contentWidth) + scrollLocation.x + spaceWidth, y + scrollLocation.y);
}
Aggregations