Java Code Examples for com.intellij.codeInsight.lookup.LookupElement#getLookupString()

The following examples show how to use com.intellij.codeInsight.lookup.LookupElement#getLookupString() . 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: StatisticsUpdate.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void addSparedChars(Lookup lookup, LookupElement item, InsertionContext context) {
  String textInserted;
  if (context.getOffsetMap().containsOffset(CompletionInitializationContext.START_OFFSET) &&
      context.getOffsetMap().containsOffset(InsertionContext.TAIL_OFFSET) &&
      context.getTailOffset() >= context.getStartOffset()) {
    textInserted = context.getDocument().getImmutableCharSequence().subSequence(context.getStartOffset(), context.getTailOffset()).toString();
  }
  else {
    textInserted = item.getLookupString();
  }
  String withoutSpaces = StringUtil.replace(textInserted, new String[]{" ", "\t", "\n"}, new String[]{"", "", ""});
  int spared = withoutSpaces.length() - lookup.itemPattern(item).length();
  char completionChar = context.getCompletionChar();

  if (!LookupEvent.isSpecialCompletionChar(completionChar) && withoutSpaces.contains(String.valueOf(completionChar))) {
    spared--;
  }
  if (spared > 0) {
    mySpared += spared;
  }
}
 
Example 2
Source File: StatisticsWeigher.java    From consulo with Apache License 2.0 6 votes vote down vote up
private TreeMap<Integer, List<LookupElement>> buildMapByWeight(Iterable<LookupElement> source) {
  MultiMap<String, LookupElement> byName = MultiMap.create();
  List<LookupElement> noStats = new ArrayList<>();
  for (LookupElement element : source) {
    String string = element.getLookupString();
    if (myStringsWithWeights.contains(string)) {
      byName.putValue(string, element);
    } else {
      noStats.add(element);
    }
  }

  TreeMap<Integer, List<LookupElement>> map = new TreeMap<>();
  map.put(0, noStats);
  for (String s : byName.keySet()) {
    List<LookupElement> group = (List<LookupElement>)byName.get(s);
    Collections.sort(group, Comparator.comparing(this::getScalarWeight).reversed());
    map.computeIfAbsent(getMaxWeight(group), __ -> new ArrayList<>()).addAll(group);
  }
  return map;
}
 
Example 3
Source File: AnnotationLightCodeInsightFixtureTestCase.java    From idea-php-annotation-plugin with MIT License 6 votes vote down vote up
protected void assertCompletionContainsDeprecationPresentation(LanguageFileType languageFileType, String configureByText, String lookupString, boolean deprecated) {
    myFixture.configureByText(languageFileType, configureByText);
    myFixture.completeBasic();

    final LookupElement[] lookupElements = myFixture.completeBasic();
    for (LookupElement lookupElement : lookupElements) {
        final String myLookupString = lookupElement.getLookupString();
        if (myLookupString.equals(lookupString)) {
            LookupElementPresentation presentation = new LookupElementPresentation();
            lookupElement.renderElement(presentation);

            assertEquals(deprecated, presentation.isStrikeout());

            return;
        }
    }

    fail("Unable to assert element presentation");
}
 
Example 4
Source File: ServiceCompletionProvider.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
@Nullable
@Override
public Comparable weigh(@NotNull LookupElement element) {
    String lookupString = element.getLookupString();
    if(!elements.contains(lookupString)) {
        return 0;
    }

    // start by "0"
    int pos = elements.indexOf(lookupString) + 1;

    // we reversed the list so, top most element has higher negative value now
    return -1000 - pos;
}
 
Example 5
Source File: CompletionUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static void emulateInsertion(LookupElement item, int offset, InsertionContext context) {
  setOffsets(context, offset, offset);

  final Editor editor = context.getEditor();
  final Document document = editor.getDocument();
  final String lookupString = item.getLookupString();

  document.insertString(offset, lookupString);
  editor.getCaretModel().moveToOffset(context.getTailOffset());
  PsiDocumentManager.getInstance(context.getProject()).commitDocument(document);
  item.handleInsert(context);
  PsiDocumentManager.getInstance(context.getProject()).doPostponedOperationsAndUnblockDocument(document);
}
 
Example 6
Source File: AnnotationTagInsertHandler.java    From idea-php-symfony2-plugin with MIT License 5 votes vote down vote up
public void handleInsert(@NotNull InsertionContext context, @NotNull LookupElement lookupElement) {
    PhpInsertHandlerUtil.insertStringAtCaret(context.getEditor(), "()");
    context.getEditor().getCaretModel().moveCaretRelatively(-1, 0, false, false, true);

    String lookupString = lookupElement.getLookupString();
    if(AnnotationIndex.getControllerAnnotations().containsKey(lookupString)) {
        String useStatement = AnnotationIndex.getControllerAnnotations().get(lookupString).getUse();
        if(null != useStatement) {
            AnnotationUseImporter.insertUse(context, useStatement);
        }
    }

}
 
Example 7
Source File: Suggestion.java    From intellij-spring-assistant with MIT License 5 votes vote down vote up
public void renderElement(LookupElement element, LookupElementPresentation presentation) {
  Suggestion suggestion = (Suggestion) element.getObject();
  if (suggestion.icon != null) {
    presentation.setIcon(suggestion.icon);
  }

  presentation.setStrikeout(suggestion.deprecationLevel != null);
  if (suggestion.deprecationLevel != null) {
    if (suggestion.deprecationLevel == SpringConfigurationMetadataDeprecationLevel.error) {
      presentation.setItemTextForeground(RED);
    } else {
      presentation.setItemTextForeground(YELLOW);
    }
  }

  String lookupString = element.getLookupString();
  presentation.setItemText(lookupString);
  if (!lookupString.equals(suggestion.suggestionToDisplay)) {
    presentation.setItemTextBold(true);
  }

  String shortDescription;
  if (suggestion.defaultValue != null) {
    shortDescription = shortenTextWithEllipsis(suggestion.defaultValue, 60, 0, true);
    TextAttributes attrs =
        EditorColorsManager.getInstance().getGlobalScheme().getAttributes(SCALAR_TEXT);
    presentation.setTailText("=" + shortDescription, attrs.getForegroundColor());
  }

  if (suggestion.description != null) {
    presentation
        .appendTailText(" (" + getFirstSentenceWithoutDot(suggestion.description) + ")",
            true);
  }

  if (suggestion.shortType != null) {
    presentation.setTypeText(suggestion.shortType);
  }
}
 
Example 8
Source File: DynamicPathCompletionTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoFunctionCompletion() throws Exception {
    configureByTestName();

    complete();

    Assert.assertNotNull(myItems);
    Assert.assertTrue("No completions found", myItems.length > 0);

    for (LookupElement item : myItems) {
        String lookupString = item.getLookupString();
        Assert.assertFalse("Completion of path must not offer function name: " + lookupString, lookupString.contains("myFunction"));
    }
}
 
Example 9
Source File: DynamicPathCompletionTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Test
public void testTildeCompletion() throws Throwable {
    configureByTestName();

    complete();

    Assert.assertNotNull(myItems);
    Assert.assertTrue("No completions for $HOME", myItems.length >= 1);

    for (LookupElement item : myItems) {
        String lookup = item.getLookupString();
        Assert.assertTrue("item does not begin with ~/ : " + lookup, lookup.startsWith("~/"));
        Assert.assertFalse("item must not begin with ~// : " + lookup, lookup.startsWith("~//"));
    }
}
 
Example 10
Source File: DynamicPathCompletionTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Test
public void testHomeVarHiddenCompletion() throws Throwable {
    configureByTestName();

    complete();

    Assert.assertNotNull(myItems);
    Assert.assertTrue("No completions for $HOME", myItems.length >= 1);

    for (LookupElement item : myItems) {
        String lookup = item.getLookupString();
        Assert.assertFalse("item must not contain ./. : " + lookup, lookup.contains("./."));
    }
}
 
Example 11
Source File: DynamicPathCompletionTest.java    From BashSupport with Apache License 2.0 5 votes vote down vote up
@Test
public void testHomeVarCompletion() throws Throwable {
    configureByTestName();

    complete();

    Assert.assertNotNull("Expected completion, but got " + myItems, myItems);
    Assert.assertTrue("No completions for $HOME", myItems.length >= 1);

    for (LookupElement item : myItems) {
        String lookup = item.getLookupString();
        Assert.assertTrue("item does not begin with $HOME/ : " + lookup, lookup.startsWith("$HOME/"));
        Assert.assertFalse("item must not begin with $HOME// : " + lookup, lookup.startsWith("$HOME//"));
    }
}
 
Example 12
Source File: FluidViewHelperReferenceInsertHandler.java    From idea-php-typo3-plugin with MIT License 5 votes vote down vote up
private ViewHelper findViewHelperByReference(@NotNull FluidViewHelperReference completionParent, @NotNull LookupElement item) {
    FluidViewHelperExpr viewHelperExpr = (FluidViewHelperExpr) completionParent.getParent();
    String boundNamespace = viewHelperExpr.getBoundNamespace().getText();
    String lookupString = item.getLookupString();

    return findByAlias(completionParent.getProject(), item.getPsiElement(), boundNamespace, lookupString);
}
 
Example 13
Source File: CSharpCompletionSorting.java    From consulo-csharp with Apache License 2.0 4 votes vote down vote up
@Nullable
@Override
@RequiredReadAction
public Type weigh(@Nonnull LookupElement element)
{
	Type type = element.getUserData(ourForceType);
	if(type != null)
	{
		return type;
	}

	IElementType keywordElementType = element.getUserData(CSharpCompletionUtil.KEYWORD_ELEMENT_TYPE);

	String lookupString = element.getLookupString();
	if(lookupString.startsWith("__") && keywordElementType != null)
	{
		return Type.hiddenKeywords;
	}
	else if(keywordElementType != null)
	{
		return Type.keyword;
	}
	else if(lookupString.startsWith("#"))
	{
		return Type.preprocessorKeywords;
	}

	PsiElement psiElement = element.getPsiElement();
	if(psiElement instanceof CSharpLocalVariable || psiElement instanceof DotNetParameter || psiElement instanceof CSharpLambdaParameter)
	{
		return Type.localVariableOrParameter;
	}

	if(psiElement instanceof CSharpFieldDeclaration && ((CSharpFieldDeclaration) psiElement).isConstant() || psiElement instanceof CSharpEnumConstantDeclaration)
	{
		return Type.constants;
	}

	if(psiElement instanceof CSharpPropertyDeclaration || psiElement instanceof CSharpEventDeclaration || psiElement instanceof CSharpFieldDeclaration || psiElement instanceof
			CSharpMethodDeclaration && !((CSharpMethodDeclaration) psiElement).isDelegate())
	{
		return Type.member;
	}

	if(psiElement instanceof DotNetNamespaceAsElement)
	{
		return Type.namespace;
	}
	return Type.any;
}
 
Example 14
Source File: HaxeControllingCompletionContributor.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
private static String getFunctionName(CompletionResult candidate) {
  LookupElement el = candidate.getLookupElement();
  String name = null != el ? el.getLookupString() : null;
  return name;
}
 
Example 15
Source File: ConfigComponentLookupElement.java    From yiistorm with MIT License 4 votes vote down vote up
@Override
public void handleInsert(InsertionContext insertionContext, LookupElement lookupElement) {
    String lookup = lookupElement.getLookupString();
    int lookup2 = insertionContext.getStartOffset();
    insertionContext.getEditor().getDocument().replaceString(lookup2, lookup2 + lookup.length(), lookup);
}
 
Example 16
Source File: MessageLookupElement.java    From yiistorm with MIT License 4 votes vote down vote up
@Override
public void handleInsert(InsertionContext insertionContext, LookupElement lookupElement) {
    String lookup = lookupElement.getLookupString();
    int lookup2 = insertionContext.getStartOffset();
    insertionContext.getEditor().getDocument().replaceString(lookup2, lookup2 + lookup.length(), lookup);
}
 
Example 17
Source File: GraphQLCompletionContributor.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
private String getSortText(LookupElement element) {
    if (element.getLookupString().startsWith("__") || element.getLookupString().startsWith("...")) {
        return "|" + element.getLookupString();
    }
    return element.getLookupString();
}
 
Example 18
Source File: DefaultCompletionStatistician.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public StatisticsInfo serialize(final LookupElement element, final CompletionLocation location) {
  return new StatisticsInfo("completion#" + location.getCompletionParameters().getOriginalFile().getLanguage(), element.getLookupString());
}