Java Code Examples for com.intellij.codeInsight.completion.CompletionParameters#getOriginalFile()

The following examples show how to use com.intellij.codeInsight.completion.CompletionParameters#getOriginalFile() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RamlCompletionContributor.java    From mule-intellij-plugins with Apache License 2.0 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters completionParameters, ProcessingContext processingContext, @NotNull CompletionResultSet completionResultSet)
{
    final int offset = completionParameters.getOffset() - 1;
    final PsiFile originalFile = completionParameters.getOriginalFile();
    if (RamlUtils.isRamlFile(originalFile))
    {
        final String text = originalFile.getText();
        System.out.println("text = " + text);
        System.out.println("offset = " + offset);
        final Suggestions suggestions = new RamlSuggester().suggestions(text, offset);
        final List<Suggestion> suggestionList = suggestions.getSuggestions();
        for (Suggestion suggestion : suggestionList)
        {
            final LookupElementBuilder map = LookupElementBuilder.create(suggestion.getValue())
                                                                 .withPresentableText(suggestion.getLabel())
                                                                 .withLookupString(suggestion.getLabel())
                                                                 .withLookupString(suggestion.getValue());
            completionResultSet.addElement(map);
        }
    }
}
 
Example 2
Source File: DebuggerCompletionContributor.java    From intellij with Apache License 2.0 6 votes vote down vote up
@Override
protected void addCompletions(
    CompletionParameters parameters, ProcessingContext context, CompletionResultSet result) {
  PsiFile file = parameters.getOriginalFile();
  SkylarkSourcePosition debugContext = getDebugContext(file);
  if (debugContext == null) {
    return;
  }
  String text = file.getText();
  List<Value> suggestions =
      SkylarkDebugCompletionSuggestions.create(debugContext).getCompletionValues(text);
  if (suggestions.isEmpty()) {
    return;
  }
  suggestions.forEach(
      value ->
          result.addElement(
              LookupElementBuilder.create(value.getLabel())
                  .withIcon(SkylarkDebugValue.getIcon(value))));
}
 
Example 3
Source File: StaticArgCompletionProvider.java    From Intellij-Plugin with Apache License 2.0 6 votes vote down vote up
@Override
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet resultSet) {
    String prefix = getPrefix(parameters);
    resultSet = resultSet.withPrefixMatcher(new PlainPrefixMatcher(prefix));
    PsiFile specFile = parameters.getOriginalFile();
    SpecDetail specDetail = PsiTreeUtil.getChildOfType(specFile, SpecDetail.class);
    List<SpecStep> stepsInFile = new ArrayList<>();
    addContextSteps(specDetail, stepsInFile);
    addStepsInScenarios(specFile, stepsInFile);

    Set<String> staticArgs = getArgsFromSteps(stepsInFile);
    for (String arg : staticArgs) {
        if (arg != null) {
            LookupElementBuilder item = LookupElementBuilder.create(arg);
            resultSet.addElement(item);
        }
    }
}
 
Example 4
Source File: LatteVariableCompletionProvider.java    From intellij-latte with MIT License 6 votes vote down vote up
@Override
protected void addCompletions(
		@NotNull CompletionParameters parameters,
		ProcessingContext context,
		@NotNull CompletionResultSet result
) {
	PsiElement element = parameters.getPosition().getParent();
	if ((element instanceof LattePhpVariable) && ((LattePhpVariable) element).isDefinition()) {
		return;
	}

	List<LookupElement> elements = attachPhpVariableCompletions(element, parameters.getOriginalFile().getVirtualFile());
	result.addAllElements(elements);

	if (parameters.getOriginalFile() instanceof LatteFile) {
		attachTemplateTypeCompletions(result, element.getProject(), (LatteFile) parameters.getOriginalFile());
	}
}
 
Example 5
Source File: CommitCompletionContributor.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void fillCompletionVariants(CompletionParameters parameters, CompletionResultSet result) {
  PsiFile file = parameters.getOriginalFile();
  Document document = PsiDocumentManager.getInstance(file.getProject()).getDocument(file);
  if (document != null) {
    DataContext dataContext = document.getUserData(CommitMessage.DATA_CONTEXT_KEY);
    if (dataContext != null) {
      result.stopHere();
      if (parameters.getInvocationCount() > 0) {
        ChangeList[] lists = dataContext.getData(VcsDataKeys.CHANGE_LISTS);
        if (lists != null) {
          String prefix = TextFieldWithAutoCompletionListProvider.getCompletionPrefix(parameters);
          CompletionResultSet insensitive = result.caseInsensitive().withPrefixMatcher(new CamelHumpMatcher(prefix));
          for (ChangeList list : lists) {
            for (Change change : list.getChanges()) {
              VirtualFile virtualFile = change.getVirtualFile();
              if (virtualFile != null) {
                LookupElementBuilder element = LookupElementBuilder.create(virtualFile.getName()).
                  withIcon(VirtualFilePresentation.getAWTIcon(virtualFile));
                insensitive.addElement(element);
              }
            }
          }
        }
      }
    }
  }
}
 
Example 6
Source File: CompletionContributorForTextField.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Override
public void fillCompletionVariants(CompletionParameters parameters, CompletionResultSet result) {
  PsiFile file = parameters.getOriginalFile();
  if (!(file instanceof PsiPlainTextFile)) return;

  TextFieldCompletionProvider field = file.getUserData(TextFieldCompletionProvider.COMPLETING_TEXT_FIELD_KEY);
  if (field == null) return;

  if (!(field instanceof DumbAware) && DumbService.isDumb(file.getProject())) return;
  
  String text = file.getText();
  int offset = Math.min(text.length(), parameters.getOffset());

  String prefix = field.getPrefix(text.substring(0, offset));

  CompletionResultSet activeResult;

  if (!result.getPrefixMatcher().getPrefix().equals(prefix)) {
    activeResult = result.withPrefixMatcher(prefix);
  }
  else {
    activeResult = result;
  }

  if (field.isCaseInsensitivity()) {
    activeResult = activeResult.caseInsensitive();
  }

  field.addCompletionVariants(text, offset, prefix, activeResult);
}
 
Example 7
Source File: JSGraphQLEndpointCompletionContributor.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
public JSGraphQLEndpointCompletionContributor() {

		CompletionProvider<CompletionParameters> provider = new CompletionProvider<CompletionParameters>() {
			@Override
			protected void addCompletions(@NotNull final CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {

				final PsiFile file = parameters.getOriginalFile();

				if (!(file instanceof JSGraphQLEndpointFile)) {
					return;
				}

				final boolean autoImport = parameters.isExtendedCompletion() || parameters.getCompletionType() == CompletionType.SMART;

				final PsiElement completionElement = Optional.ofNullable(parameters.getOriginalPosition()).orElse(parameters.getPosition());
				if (completionElement != null) {

					final PsiElement parent = completionElement.getParent();
					final PsiElement leafBeforeCompletion = PsiTreeUtil.prevVisibleLeaf(completionElement);

					// 1. complete on interface name after IMPLEMENTS token
					if (completeImplementableInterface(result, autoImport, completionElement, leafBeforeCompletion)) {
						return;
					}

					// 2. import file
					if (completeImportFile(result, file, parent)) {
						return;
					}

					// 3.A. top level completions, e.g. keywords and definition annotations
					if (completeKeywordsAndDefinitionAnnotations(result, autoImport, completionElement, leafBeforeCompletion, parent)) {
						return;
					}

					// 3.B. implements when type definition surrounds completion element (e.g. when it has a FieldDefinitionSet)
					if (completeImplementsInsideTypeDefinition(result, completionElement, parent)) {
						return;
					}

					// 4. completions inside FieldDefinitionSet
					final JSGraphQLEndpointFieldDefinitionSet fieldDefinitionSet = PsiTreeUtil.getParentOfType(completionElement, JSGraphQLEndpointFieldDefinitionSet.class);
					if (fieldDefinitionSet != null) {

						// 4.A. field/argument type completion
						if (completeFieldOrArgumentType(result, autoImport, completionElement)) {
							return;
						}

						// 4.B. annotations
						if (completeAnnotations(result, autoImport, file, completionElement)) {
							return;
						}

						// 4.C. annotation arguments
						if (completeAnnotationArguments(result, file, completionElement, leafBeforeCompletion)) {
							return;
						}

						// 4.D. override for interface fields
						if (completeOverrideFields(fieldDefinitionSet, completionElement, result)) {
							return;
						}
					}

					// 5. completions inside SchemaDefinition
					if (completeInsideSchemaDefinition(result, completionElement, leafBeforeCompletion)) {
						return;
					}

				}

			}
		};

		extend(CompletionType.BASIC, PlatformPatterns.psiElement(), provider);
		extend(CompletionType.SMART, PlatformPatterns.psiElement(), provider);

	}
 
Example 8
Source File: JSGraphQLEndpointDocCompletionContributor.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
public JSGraphQLEndpointDocCompletionContributor() {

		CompletionProvider<CompletionParameters> provider = new CompletionProvider<CompletionParameters>() {
			@SuppressWarnings("unchecked")
			@Override
			protected void addCompletions(@NotNull final CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {

				final PsiFile file = parameters.getOriginalFile();

				if (!(file instanceof JSGraphQLEndpointDocFile)) {
					return;
				}

				final PsiElement completionElement = Optional.ofNullable(parameters.getOriginalPosition()).orElse(parameters.getPosition());
				if (completionElement != null) {
					final PsiComment comment = PsiTreeUtil.getContextOfType(completionElement, PsiComment.class);
					if (comment != null && JSGraphQLEndpointDocPsiUtil.isDocumentationComment(comment)) {

						if (completionElement.getNode().getElementType() == JSGraphQLEndpointDocTokenTypes.DOCVALUE) {
							final JSGraphQLEndpointFieldDefinition fieldDefinition = PsiTreeUtil.getNextSiblingOfType(comment, JSGraphQLEndpointFieldDefinition.class);
							if (fieldDefinition != null && fieldDefinition.getArgumentsDefinition() != null) {
								final List<String> otherDocTagValues = JSGraphQLEndpointDocPsiUtil.getOtherDocTagValues(comment);
								for (JSGraphQLEndpointInputValueDefinition arg : PsiTreeUtil.findChildrenOfType(fieldDefinition.getArgumentsDefinition(), JSGraphQLEndpointInputValueDefinition.class)) {
									final String argName = arg.getInputValueDefinitionIdentifier().getText();
									if (!otherDocTagValues.contains(argName)) {
										result.addElement(LookupElementBuilder.create(argName).withInsertHandler(AddSpaceInsertHandler.INSTANCE));
									}
								}
							}
							return;
						}

						final JSGraphQLEndpointDocTag tagBefore = PsiTreeUtil.getPrevSiblingOfType(completionElement, JSGraphQLEndpointDocTag.class);
						final JSGraphQLEndpointDocTag tagParent = PsiTreeUtil.getParentOfType(completionElement, JSGraphQLEndpointDocTag.class);
						if (tagBefore == null || tagParent != null) {
							String completion = "param";
							final boolean includeAt = completionElement.getNode().getElementType() != JSGraphQLEndpointDocTokenTypes.DOCNAME;
							if (includeAt) {
								completion = "@" + completion;
							}
							result.addElement(LookupElementBuilder.create(completion).withInsertHandler(AddSpaceInsertHandler.INSTANCE_WITH_AUTO_POPUP));
						}
					}
				}
			}
		};

		extend(CompletionType.BASIC, PlatformPatterns.psiElement(), provider);

	}
 
Example 9
Source File: BuckTargetCompletionContributor.java    From buck with Apache License 2.0 4 votes vote down vote up
@Override
public void fillCompletionVariants(
    @NotNull CompletionParameters parameters, @NotNull CompletionResultSet result) {
  PsiFile psiFile = parameters.getOriginalFile();
  if (!BuckFileType.INSTANCE.equals(psiFile.getFileType())) {
    return;
  }
  PsiElement position = parameters.getPosition();
  String openingQuote;
  if (BuckPsiUtils.hasElementType(position, BuckTypes.APOSTROPHED_STRING)) {
    openingQuote = "'";
  } else if (BuckPsiUtils.hasElementType(position, BuckTypes.APOSTROPHED_RAW_STRING)) {
    openingQuote = "r'";
  } else if (BuckPsiUtils.hasElementType(position, BuckTypes.TRIPLE_APOSTROPHED_STRING)) {
    openingQuote = "'''";
  } else if (BuckPsiUtils.hasElementType(position, BuckTypes.TRIPLE_APOSTROPHED_RAW_STRING)) {
    openingQuote = "r'''";
  } else if (BuckPsiUtils.hasElementType(position, BuckTypes.QUOTED_STRING)) {
    openingQuote = "\"";
  } else if (BuckPsiUtils.hasElementType(position, BuckTypes.QUOTED_RAW_STRING)) {
    openingQuote = "r\"";
  } else if (BuckPsiUtils.hasElementType(position, BuckTypes.TRIPLE_QUOTED_STRING)) {
    openingQuote = "\"\"\"";
  } else if (BuckPsiUtils.hasElementType(position, BuckTypes.TRIPLE_QUOTED_RAW_STRING)) {
    openingQuote = "r\"\"\"";
  } else {
    return;
  }
  Project project = position.getProject();
  VirtualFile virtualFile = psiFile.getVirtualFile();
  String positionStringWithQuotes = position.getText();
  String prefix =
      positionStringWithQuotes.substring(
          openingQuote.length(), parameters.getOffset() - position.getTextOffset());
  if (BuckPsiUtils.findAncestorWithType(position, BuckTypes.LOAD_TARGET_ARGUMENT) != null) {
    // Inside a load target, extension files are "@this//syntax/points:to/files.bzl"
    if (prefix.startsWith("@")) {
      prefix = prefix.substring(1);
    }
    doCellNames(position, prefix, result);
    doTargetsForRelativeExtensionFile(virtualFile, prefix, result);
    doPathsForFullyQualifiedExtensionFile(virtualFile, project, prefix, result);
    doTargetsForFullyQualifiedExtensionFile(virtualFile, project, prefix, result);
  } else if (BuckPsiUtils.findAncestorWithType(position, BuckTypes.LOAD_ARGUMENT) != null) {
    doSymbolsFromExtensionFile(virtualFile, project, position, prefix, result);
  } else {
    if (prefix.startsWith("@")) {
      prefix = prefix.substring(1);
    }
    doCellNames(position, prefix, result);
    doTargetsForRelativeBuckTarget(psiFile, prefix, result);
    doPathsForFullyQualifiedBuckTarget(virtualFile, project, prefix, result);
    doTargetsForFullyQualifiedBuckTarget(virtualFile, project, prefix, result);
  }
}