Java Code Examples for com.intellij.psi.PsiElement#getTextLength()

The following examples show how to use com.intellij.psi.PsiElement#getTextLength() . 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: FluidInjector.java    From idea-php-typo3-plugin with MIT License 6 votes vote down vote up
@Override
public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull PsiElement context) {
    if (context.getLanguage() == XMLLanguage.INSTANCE) return;
    final Project project = context.getProject();
    if (!FluidIndexUtil.hasFluid(project)) return;

    final PsiElement parent = context.getParent();
    if (context instanceof XmlAttributeValueImpl && parent instanceof XmlAttribute) {
        final int length = context.getTextLength();
        final String name = ((XmlAttribute) parent).getName();

        if (isInjectableAttribute(project, length, name)) {
            registrar
                .startInjecting(FluidLanguage.INSTANCE)
                .addPlace(null, null, (PsiLanguageInjectionHost) context, ElementManipulators.getValueTextRange(context))
                .doneInjecting();
        }
    }
}
 
Example 2
Source File: Identikit.java    From consulo with Apache License 2.0 6 votes vote down vote up
public PsiElement findInside(@Nonnull PsiElement element, int startOffset, int endOffset) {
  PsiElement anchor = AbstractFileViewProvider.findElementAt(element, startOffset); // finds child in this tree only, unlike PsiElement.findElementAt()
  if (anchor == null && startOffset == element.getTextLength()) {
    anchor = PsiTreeUtil.getDeepestLast(element);
  }
  if (anchor == null) return null;

  PsiElement result = findParent(startOffset, endOffset, anchor);
  if (endOffset == startOffset) {
    while ((result == null || result.getTextRange().getStartOffset() != startOffset) && anchor.getTextRange().getStartOffset() == endOffset) {
      anchor = PsiTreeUtil.prevLeaf(anchor, false);
      if (anchor == null) break;

      result = findParent(startOffset, endOffset, anchor);
    }
  }
  return result;

}
 
Example 3
Source File: ParameterInfoController.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static int getParameterIndex(@Nonnull PsiElement[] parameters, @Nonnull IElementType delimiter, int offset) {
  for (int i = 0; i < parameters.length; i++) {
    PsiElement parameter = parameters[i];
    TextRange textRange = parameter.getTextRange();
    int startOffset = textRange.getStartOffset();
    if (offset < startOffset) {
      if (i == 0) return 0;
      PsiElement elementInBetween = parameters[i - 1];
      int currOffset = elementInBetween.getTextRange().getEndOffset();
      while ((elementInBetween = PsiTreeUtil.nextLeaf(elementInBetween)) != null) {
        if (currOffset >= startOffset) break;
        ASTNode node = elementInBetween.getNode();
        if (node != null && node.getElementType() == delimiter) {
          return offset <= currOffset ? i - 1 : i;
        }
        currOffset += elementInBetween.getTextLength();
      }
      return i;
    }
    else if (offset <= textRange.getEndOffset()) {
      return i;
    }
  }
  return Math.max(0, parameters.length - 1);
}
 
Example 4
Source File: CsvHelper.java    From intellij-csv-validator with Apache License 2.0 5 votes vote down vote up
public static int getFieldStartOffset(PsiElement field) {
    PsiElement separator = CsvHelper.getPreviousSeparator(field);
    if (separator == null) {
        separator = getPreviousCRLF(field.getParent());
    }
    return separator == null ? 0 : separator.getTextOffset() + separator.getTextLength();
}
 
Example 5
Source File: DuplocatorUtil.java    From consulo with Apache License 2.0 5 votes vote down vote up
public static boolean shouldSkip(PsiElement element, PsiElement elementToMatchWith) {
  if (element == null || elementToMatchWith == null) {
    return false;
  }

  if (element.getClass() == elementToMatchWith.getClass()) {
    return false;
  }

  if (element.getFirstChild() == null && element.getTextLength() == 0 && !(element instanceof LeafElement)) {
    return true;
  }

  return false;
}
 
Example 6
Source File: XQueryPsiImplUtil.java    From intellij-xquery with Apache License 2.0 5 votes vote down vote up
public static PsiElement getPrevNonWhiteSpaceElement(PsiElement element) {
    PsiElement previousLeaf = previousLeaf(element);
    while (previousLeaf != null && (previousLeaf.getTextLength() == 0 || isWhitespace(previousLeaf))) {
        previousLeaf = previousLeaf(previousLeaf);
    }
    return previousLeaf;
}
 
Example 7
Source File: CSharpPreprocessorReferenceExpressionImpl.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredReadAction
@Override
public TextRange getRangeInElement()
{
	PsiElement element = getElement();
	return new TextRange(0, element.getTextLength());
}
 
Example 8
Source File: CSharpReferenceExpressionImplUtil.java    From consulo-csharp with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
public static TextRange getRangeInElement(@Nonnull CSharpReferenceExpression referenceExpression)
{
	PsiElement referenceElement = referenceExpression.getReferenceElement();
	if(referenceElement == null)
	{
		return TextRange.EMPTY_RANGE;
	}

	int startOffset = referenceElement.getStartOffsetInParent();
	return new TextRange(startOffset, referenceElement.getTextLength() + startOffset);
}
 
Example 9
Source File: GLSLDefineDirective.java    From glsl4idea with GNU Lesser General Public License v3.0 5 votes vote down vote up
/**
 * @return text after the name
 */
@NotNull
public String getBoundText(){
    final PsiElement name = getNameIdentifier();
    if(name == null)return "";

    int textStart = name.getTextOffset() + name.getTextLength();
    int textEnd = getTextOffset() + getTextLength();
    final String text = getContainingFile().getText();
    if(textStart >= textEnd || textStart < 0 || textEnd > text.length()) return "";
    return text.substring(textStart, textEnd).trim();
}
 
Example 10
Source File: ConvertArgTypeIntentionBase.java    From Intellij-Plugin with Apache License 2.0 5 votes vote down vote up
@Override
public void invoke(@NotNull Project project, Editor editor, @NotNull PsiElement element) throws IncorrectOperationException {
    PsiElement arg = PsiTreeUtil.getParentOfType(element, SpecArg.class);
    if (arg == null)
        arg = PsiTreeUtil.getParentOfType(element, ConceptArg.class);
    if (arg == null)
        return;
    String text = arg.getText();
    String paramText = StringUtils.substring(text, 1, text.length() - 1);
    String newText = getReplacementString(paramText);
    int startOffset = arg.getTextOffset();
    int endOffset = startOffset + arg.getTextLength();
    editor.getDocument().replaceString(startOffset, endOffset, newText);
    editor.getSelectionModel().setSelection(startOffset + 1, endOffset - 1);
}
 
Example 11
Source File: Unity3dKeyValueReferenceBase.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
@RequiredReadAction
@Nonnull
@Override
public TextRange getRangeInElement()
{
	PsiElement key = myKeyValue.getKey();
	if(key == null)
	{
		return new TextRange(0, myKeyValue.getTextLength());
	}
	// cut :
	return new TextRange(0, key.getTextLength() - 1);
}
 
Example 12
Source File: ShaderReference.java    From consulo-unity3d with Apache License 2.0 5 votes vote down vote up
@Nonnull
@RequiredReadAction
@Override
public TextRange getRangeInElement()
{
	PsiElement referenceElement = getReferenceElement();
	return new TextRange(0, referenceElement.getTextLength());
}
 
Example 13
Source File: ProjectViewKeywordCompletionContributor.java    From intellij with Apache License 2.0 5 votes vote down vote up
@Nullable
private static String findNextTokenText(final InsertionContext context) {
  final PsiFile file = context.getFile();
  PsiElement element = file.findElementAt(context.getTailOffset());
  while (element != null && element.getTextLength() == 0) {
    ASTNode next = element.getNode().getTreeNext();
    element = next != null ? next.getPsi() : null;
  }
  return element != null ? element.getText() : null;
}
 
Example 14
Source File: AbstractCypherConverter.java    From jetbrains-plugin-graph-database-support with Apache License 2.0 5 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    super.visitElement(element);

    String newValue = convert(element);
    if (newValue != null) {
        int startOffset = element.getTextRange().getStartOffset() + delta;
        int endOffset = element.getTextRange().getEndOffset() + delta;
        document.replaceString(startOffset, endOffset, newValue);
        delta += newValue.length() - element.getTextLength();
    }
}
 
Example 15
Source File: CodeFragmentUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static boolean elementFit(final PsiElement element, final int start, final int end) {
  return element != null && start <= element.getTextOffset() && element.getTextOffset() + element.getTextLength() <= end;
}
 
Example 16
Source File: DocCommentProcessor.java    From markdown-doclet with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void visitElement(PsiElement element) {
    if ( element instanceof PsiDocTag ) {
        PsiDocTag tag = (PsiDocTag)element;
        String tagName = null;
        switch ( tag.getName() ) {
            case "link":
            case "linkplain":
            case "see":
                // todo: @ssee
                tagName = tag.getName();
        }
        if ( tagName != null ) {
            int inlineOffset = (tag instanceof PsiInlineDocTag) ? 1 : 0;
            String linkText = tag.getText().substring(inlineOffset + 1 + tagName.length(), tag.getTextLength() - inlineOffset).trim();
            if ( !linkText.startsWith("#") ) {
                return;
            }
            StringBuilder newLink = new StringBuilder(100);
            if ( inlineOffset > 0 ) {
                newLink.append('{');
            }
            newLink.append('@').append(tagName).append(' ');
            int refEndIndex = JavaDocUtil.extractReference(linkText);
            String refText = linkText.substring(0, refEndIndex);
            PsiElement target = JavaDocUtil.findReferenceTarget(docContext.getManager(), refText, docContext, true);
            if ( target == null ) {
                return;
            }
            newLink.append(JavaDocUtil.getReferenceText(project, target)).append(' ');
            String labelText = linkText.substring(refEndIndex).trim();
            if ( labelText.isEmpty() ) {
                labelText = JavaDocUtil.getLabelText(project, docContext.getManager(), refText, docContext);
            }
            newLink.append(labelText);
            if ( inlineOffset > 0 ) {
                newLink.append('}');
            }
            int start = getStartOffsetInComment(element);
            if ( buffer == null ) {
                buffer = new StringBuilder(docText.length() + 100);
            }
            buffer.append(docText, docTextPosition, start);
            buffer.append(newLink);
            docTextPosition += start - docTextPosition + element.getTextLength();
        }
    }
    element.acceptChildren(this);
}
 
Example 17
Source File: PsiViewerDialog.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void valueChanged(TreeSelectionEvent e) {
  if (myIgnoreBlockTreeSelectionMarker > 0 || myBlockTreeBuilder == null) {
    return;
  }

  Set<?> blockElementsSet = myBlockTreeBuilder.getSelectedElements();
  if (blockElementsSet.isEmpty()) return;
  BlockTreeNode descriptor = (BlockTreeNode)blockElementsSet.iterator().next();
  PsiElement rootPsi = ((ViewerTreeStructure)myPsiTreeBuilder.getTreeStructure()).getRootPsiElement();
  int blockStart = descriptor.getBlock().getTextRange().getStartOffset();
  PsiElement currentPsiEl = InjectedLanguageUtil.findElementAtNoCommit(rootPsi.getContainingFile(), blockStart);
  int blockLength = descriptor.getBlock().getTextRange().getLength();
  while (currentPsiEl.getParent() != null &&
         currentPsiEl.getTextRange().getStartOffset() == blockStart &&
         currentPsiEl.getTextLength() != blockLength) {
    currentPsiEl = currentPsiEl.getParent();
  }
  final BlockTreeStructure treeStructure = (BlockTreeStructure)myBlockTreeBuilder.getTreeStructure();
  BlockTreeNode rootBlockNode = treeStructure.getRootElement();
  int baseOffset = 0;
  if (rootBlockNode != null) {
    baseOffset = rootBlockNode.getBlock().getTextRange().getStartOffset();
  }
  if (currentPsiEl != null) {
    TextRange range = descriptor.getBlock().getTextRange();
    range = range.shiftRight(-baseOffset);
    int start = range.getStartOffset();
    int end = range.getEndOffset();
    final int textLength = myEditor.getDocument().getTextLength();

    if (myBlockTree.hasFocus()) {
      clearSelection();
      if (end <= textLength) {
        myHighlighter = myEditor.getMarkupModel()
          .addRangeHighlighter(start, end, HighlighterLayer.LAST, myAttributes, HighlighterTargetArea.EXACT_RANGE);
        updateIntersectHighlighter(start, end);

        myEditor.getCaretModel().moveToOffset(start);
        myEditor.getScrollingModel().scrollToCaret(ScrollType.MAKE_VISIBLE);
      }
    }
    updateReferences(currentPsiEl);
    if (!myPsiTree.hasFocus()) {
      myPsiTreeBuilder.select(currentPsiEl);
    }
  }
}
 
Example 18
Source File: AddTokenDefinitionFix.java    From intellij-plugin-v4 with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
static TextRange getRange(PsiElement elementAt, int tokenExprTextOffset) {
    return new TextRange(tokenExprTextOffset, elementAt.getTextLength() - 1);
}
 
Example 19
Source File: TreeElement.java    From consulo with Apache License 2.0 4 votes vote down vote up
public boolean textMatches(@Nonnull PsiElement element) {
  return getTextLength() == element.getTextLength() && textMatches(element.getText());
}
 
Example 20
Source File: AstBufferUtil.java    From consulo with Apache License 2.0 4 votes vote down vote up
public BufferVisitor(PsiElement element, boolean skipWhitespace, boolean skipComments) {
  this(skipWhitespace, skipComments, 0, new char[element.getTextLength()]);
  ((TreeElement)element.getNode()).acceptTree(this);
}