com.jetbrains.commandInterface.commandLine.psi.impl.CommandLinePsiImplUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of python-community Show documentation
Show all versions of python-community Show documentation
A packaging of the IntelliJ Community Edition python-community library.
This is release number 1 of trunk branch 142.
The newest version!
/*
* Copyright 2000-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jetbrains.commandInterface.commandLine.psi.impl;
import com.intellij.psi.PsiElement;
import com.jetbrains.commandInterface.command.Argument;
import com.jetbrains.commandInterface.command.Help;
import com.jetbrains.commandInterface.command.Option;
import com.jetbrains.commandInterface.commandLine.CommandLinePart;
import com.jetbrains.commandInterface.commandLine.ValidationResult;
import com.jetbrains.commandInterface.commandLine.psi.CommandLineArgument;
import com.jetbrains.commandInterface.commandLine.psi.CommandLineFile;
import com.jetbrains.commandInterface.commandLine.psi.CommandLineOption;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* Class to be used by autogenerated PSI elements to delegate logic
*
* @author Ilya.Kazakevich
*/
@SuppressWarnings("StaticMethodOnlyUsedInOneClass") // This class was created to store functions for grammar kit
final class CommandLinePsiImplUtils {
private CommandLinePsiImplUtils() {
}
/**
* Checks if option is long (--long-option vs -s)
*
* @param o option
* @return true if long
*/
static boolean isLong(@NotNull final CommandLineOption o) {
return o.getLongOptionNameToken() != null;
}
/**
* Finds real option based on psi opton
*
* @param option psi option
* @return real option (if any)
*/
@Nullable
static Option findRealOption(@NotNull final CommandLineOption option) {
final ValidationResult validationResult = getValidationResult(option);
if (validationResult == null) {
return null;
}
return validationResult.getOption(option);
}
/**
* 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);
}
/**
* Finds real argument based on psi argument
*
* @param argument psi argument
* @return real argument (if any)
*/
@Nullable
static Argument findRealArgument(@NotNull final CommandLineArgument argument) {
final ValidationResult validationResult = getValidationResult(argument);
if (validationResult == null) {
return null;
}
return validationResult.getArgument(argument);
}
/**
* Finds option if argument is option argument
*
* @param argument argument to check
* @return option (if option argument) or null if not
*/
@Nullable
static Option findOptionForOptionArgument(@NotNull final CommandLineArgument argument) {
final ValidationResult validationResult = getValidationResult(argument);
if (validationResult == null) {
return null;
}
return validationResult.getOptionForOptionArgument(argument);
}
/**
* Searches for validation result for command line
*
* @param commandLinePart command line part
* @return validation result (if any)
*/
@Nullable
private static ValidationResult getValidationResult(@NotNull final CommandLinePart commandLinePart) {
final CommandLineFile commandLineFile = commandLinePart.getCommandLineFile();
if (commandLineFile == null) {
return null;
}
final ValidationResult validationResult = commandLineFile.getValidationResult();
if (validationResult == null) {
return null;
}
return validationResult;
}
/**
* Returns option name regardless it is long or short.
*
* @param o option
* @return name (if any)
*/
@Nullable
@NonNls
static String getOptionName(@NotNull final CommandLineOption o) {
final PsiElement longNameToken = o.getLongOptionNameToken();
if (longNameToken != null) {
return longNameToken.getText();
}
final PsiElement shortOptionNameToken = o.getShortOptionNameToken();
if (shortOptionNameToken != null) {
return shortOptionNameToken.getText();
}
return null;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy