All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.jetbrains.commandInterface.commandLine.psi.ValidationResultImpl Maven / Gradle / Ivy

Go to download

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;

import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiWhiteSpace;
import com.intellij.util.containers.hash.HashMap;
import com.jetbrains.commandInterface.commandLine.ValidationResult;
import com.jetbrains.commandInterface.command.Argument;
import com.jetbrains.commandInterface.command.Command;
import com.jetbrains.commandInterface.command.Option;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

/**
 * Validation result provider and holder implemented as visitor.
 * @author Ilya.Kazakevich
 */
final class ValidationResultImpl extends CommandLineVisitor implements ValidationResult {
  /**
   * All options [name -> option]
   */
  @NotNull
  private final Map myOptions = new HashMap();
  /**
   * Available, but unused options [name -> option]
   */
  @NotNull
  private final Map myUnusedOptions = new HashMap();
  /**
   * We always need command to validate args
   */
  @NotNull
  private final Command myCommand;
  /**
   * Number of next positional argument. I.e. will be 3 for "my_arg arg_1 arg_2"
   */
  private int myCurrentPositionArgument;
  /**
   * If next arg is supposed to be option arg, then option and number of expected args stored here.
   * Null stored otherwise.
   */
  @Nullable
  private Pair myCurrentOptionAndArgsLeft;
  /**
   * List of elements whose values are known to be bad
   */
  @NotNull
  private final Collection myBadValues = new ArrayList();
  /**
   * List of elements which is known to be excess
   */
  @NotNull
  private final Collection myExcessArguments = new ArrayList();
  /**
   * Map of arguments known to be option arguments [PSI argument -> option]
   */
  @NotNull
  private final Map myOptionArguments = new HashMap();
  /**
   * PSI argument -> argument map
   */
  @NotNull
  private final Map myArguments = new HashMap();

  private ValidationResultImpl(@NotNull final Command command) {
    for (final Option option : command.getOptions()) {
      for (final String optionName : option.getAllNames()) {
        myOptions.put(optionName, option);
      }
    }
    myUnusedOptions.putAll(myOptions);;
    myCommand = command;
  }

  @Override
  public boolean isBadValue(@NotNull final PsiElement element) {
    return myBadValues.contains(element);
  }

  @Override
  public boolean isExcessArgument(@NotNull final CommandLineArgument argument) {
    return myExcessArguments.contains(argument);
  }

  @Override
  @NotNull
  public Collection




© 2015 - 2025 Weber Informatics LLC | Privacy Policy