Java Code Examples for com.intellij.openapi.util.TextRange#from()

The following examples show how to use com.intellij.openapi.util.TextRange#from() . 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: BashWordImpl.java    From BashSupport with Apache License 2.0 6 votes vote down vote up
@NotNull
public TextRange getTextContentRange() {
    if (!isWrapped()) {
        return TextRange.from(0, getTextLength());
    }

    ASTNode node = getNode();
    String first = node.getFirstChildNode().getText();
    String last = node.getLastChildNode().getText();

    int textLength = getTextLength();

    if (first.startsWith("$'") && last.endsWith("'")) {
        return TextRange.from(2, textLength - 3);
    }

    return TextRange.from(1, textLength - 2);
}
 
Example 2
Source File: UsageViewTreeCellRenderer.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Nonnull
RowLocation isRowVisible(int row, @Nonnull Rectangle visibleRect) {
  Dimension pref;
  if (cachedPreferredSize == null) {
    cachedPreferredSize = pref = getPreferredSize();
  }
  else {
    pref = cachedPreferredSize;
  }
  pref.width = Math.max(visibleRect.width, pref.width);
  myRowBoundsCalled = true;
  JTree tree = getTree();
  final Rectangle bounds = tree == null ? null : tree.getRowBounds(row);
  myRowBoundsCalled = false;
  if (bounds != null) {
    myRowHeight = bounds.height;
  }
  int y = bounds == null ? 0 : bounds.y;
  TextRange vis = TextRange.from(Math.max(0, visibleRect.y - pref.height), visibleRect.height + pref.height * 2);
  boolean inside = vis.contains(y);
  if (inside) {
    return RowLocation.INSIDE_VISIBLE_RECT;
  }
  return y < vis.getStartOffset() ? RowLocation.BEFORE_VISIBLE_RECT : RowLocation.AFTER_VISIBLE_RECT;
}
 
Example 3
Source File: GraphQLReferenceService.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@NotNull
private PsiReferenceBase<GraphQLReferencePsiElement> createReference(GraphQLReferencePsiElement fromElement, PsiElement resolvedElement) {
    return new PsiReferenceBase<GraphQLReferencePsiElement>(fromElement, TextRange.from(0, fromElement.getTextLength())) {
        @Override
        public PsiElement resolve() {
            return resolvedElement;
        }

        @NotNull
        @Override
        public Object[] getVariants() {
            return PsiReference.EMPTY_ARRAY;
        }
    };
}
 
Example 4
Source File: FoldingUpdate.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void getFoldingsFor(@Nonnull PsiFile file, @Nonnull Document document, @Nonnull List<? super RegionInfo> elementsToFold, boolean quick) {
  final FileViewProvider viewProvider = file.getViewProvider();
  TextRange docRange = TextRange.from(0, document.getTextLength());
  Comparator<Language> preferBaseLanguage = Comparator.comparing((Language l) -> l != viewProvider.getBaseLanguage());
  List<Language> languages = ContainerUtil.sorted(viewProvider.getLanguages(), preferBaseLanguage.thenComparing(Language::getID));

  DocumentEx copyDoc = languages.size() > 1 ? new DocumentImpl(document.getImmutableCharSequence()) : null;
  List<RangeMarker> hardRefToRangeMarkers = new ArrayList<>();

  for (Language language : languages) {
    final PsiFile psi = viewProvider.getPsi(language);
    final FoldingBuilder foldingBuilder = LanguageFolding.INSTANCE.forLanguage(language);
    if (psi != null && foldingBuilder != null) {
      for (FoldingDescriptor descriptor : LanguageFolding.buildFoldingDescriptors(foldingBuilder, psi, document, quick)) {
        PsiElement psiElement = descriptor.getElement().getPsi();
        if (psiElement == null) {
          LOG.error("No PSI for folding descriptor " + descriptor);
          continue;
        }
        TextRange range = descriptor.getRange();
        if (!docRange.contains(range)) {
          diagnoseIncorrectRange(psi, document, language, foldingBuilder, descriptor, psiElement);
          continue;
        }

        if (copyDoc != null && !addNonConflictingRegion(copyDoc, range, hardRefToRangeMarkers)) {
          continue;
        }

        RegionInfo regionInfo = new RegionInfo(descriptor, psiElement, foldingBuilder);
        elementsToFold.add(regionInfo);
      }
    }
  }
}
 
Example 5
Source File: FileStatusMap.java    From consulo with Apache License 2.0 5 votes vote down vote up
/**
 * @param editor
 * @param passId
 * @return null means the file is clean
 */
@Nullable
// used in scala
public static TextRange getDirtyTextRange(@Nonnull Editor editor, int passId) {
  Document document = editor.getDocument();

  FileStatusMap me = DaemonCodeAnalyzerEx.getInstanceEx(editor.getProject()).getFileStatusMap();
  TextRange dirtyScope = me.getFileDirtyScope(document, passId);
  if (dirtyScope == null) return null;
  TextRange documentRange = TextRange.from(0, document.getTextLength());
  return documentRange.intersection(dirtyScope);
}
 
Example 6
Source File: MuleLanguageInjector.java    From mule-intellij-plugins with Apache License 2.0 5 votes vote down vote up
private void injectLanguage(@NotNull PsiLanguageInjectionHost host, @NotNull InjectedLanguagePlaces injectedLanguagePlaces, String scriptingName) {
    final Language requiredLanguage = Language.findLanguageByID(scriptingName);
    if (requiredLanguage != null) {
        final TextRange range = TextRange.from(0, host.getTextRange().getLength());
        injectedLanguagePlaces.addPlace(requiredLanguage, range, null, null);
    }
}
 
Example 7
Source File: SQFVariableReference.java    From arma-intellij-plugin with MIT License 4 votes vote down vote up
@Override
public TextRange getRangeInElement() {
	return TextRange.from(0, variable.getTextLength());
}
 
Example 8
Source File: SQFString.java    From arma-intellij-plugin with MIT License 4 votes vote down vote up
/**
 * @return TextRange that doesn't include the quotes and the range is relative to the <b>element</b>
 * @see #getNonQuoteRangeRelativeToFile()
 */
@NotNull
public TextRange getNonQuoteRangeRelativeToElement() {
	return TextRange.from(1, getTextLength() - 2);
}
 
Example 9
Source File: BashShebangImpl.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@Override
@NotNull
public TextRange commandAndParamsRange() {
    return TextRange.from(getShellCommandOffset(), shellCommand(true).length());
}
 
Example 10
Source File: HaskellConidBaseImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@NotNull
public PsiReference getReference() {
  return new HaskellReference(this, TextRange.from(0, getName().length()));
}
 
Example 11
Source File: LiteralTextEscaper.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nonnull
public TextRange getRelevantTextRange() {
  return TextRange.from(0, myHost.getTextLength());
}
 
Example 12
Source File: ElementManipulators.java    From consulo with Apache License 2.0 4 votes vote down vote up
public static TextRange getValueTextRange(final PsiElement element) {
  final ElementManipulator<PsiElement> manipulator = getManipulator(element);
  return manipulator == null ? TextRange.from(0, element.getTextLength()) : manipulator.getRangeInElement(element);
}
 
Example 13
Source File: XQDocHighlighter.java    From intellij-xquery with Apache License 2.0 4 votes vote down vote up
private void highlightTextRange(AnnotationHolder holder, int absoluteOffset, int length) {
    TextRange range = TextRange.from(absoluteOffset, length);
    setHighlighting(range, holder, DOC_COMMENT_TAG);
}
 
Example 14
Source File: JSGraphQLEndpointPropertyPsiElement.java    From js-graphql-intellij-plugin with MIT License 4 votes vote down vote up
@Override
public PsiReference getReference() {
	final JSGraphQLEndpointPropertyPsiElement self = this;
	final PsiElement nameIdentifier = getNameIdentifier();
	if(nameIdentifier != null) {
		return new PsiReferenceBase<PsiElement>(this, TextRange.from(nameIdentifier.getTextOffset() - self.getTextOffset(), nameIdentifier.getTextLength())) {
			@Nullable
			@Override
			public PsiElement resolve() {
				// the property may belong to a field that was declared in an implemented interface
				final JSGraphQLEndpointObjectTypeDefinition typeDefinition = PsiTreeUtil.getParentOfType(self, JSGraphQLEndpointObjectTypeDefinition.class);
				if(typeDefinition != null && typeDefinition.getImplementsInterfaces() != null) {
					final JSGraphQLEndpointNamedType[] implementedTypes = PsiTreeUtil.getChildrenOfType(typeDefinition.getImplementsInterfaces(), JSGraphQLEndpointNamedType.class);
					if(implementedTypes != null) {
						for (JSGraphQLEndpointNamedType implementedType : implementedTypes) {
							final PsiReference reference = implementedType.getReference();
							final PsiElement resolvedType = reference.resolve();
							if(resolvedType != null && resolvedType.getParent() instanceof JSGraphQLEndpointInterfaceTypeDefinition) {
								Ref<JSGraphQLEndpointPropertyPsiElement> result = new Ref<>(null);
								resolvedType.getParent().accept(new PsiRecursiveElementVisitor() {
									@Override
									public void visitElement(PsiElement element) {
										if(result.get() != null) {
											return;
										}
										if(element instanceof JSGraphQLEndpointPropertyPsiElement) {
											final JSGraphQLEndpointPropertyPsiElement interfaceProperty = (JSGraphQLEndpointPropertyPsiElement) element;
											final String name = interfaceProperty.getName();
											if(nameIdentifier.getText().equals(name)) {
												result.set(interfaceProperty);
											}
										}
										super.visitElement(element);
									}
								});
								return result.get();
							}
						}
					}
				}
				return null;
			}

			@NotNull
			@Override
			public Object[] getVariants() {
				return NO_VARIANTS;
			}

			@Override
			public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
				return self.setName(newElementName);
			}
		};
	}
	return null;
}
 
Example 15
Source File: HaskellQqblobManipulator.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
private static TextRange pairToTextRange(Pair<Integer, Integer> pair) {
    final int start = Math.max(pair.first, 0);
    final int end = Math.max(pair.second, start);
    return TextRange.from(start, end);
}
 
Example 16
Source File: BashExpansionImpl.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@NotNull
public TextRange getTextContentRange() {
    return TextRange.from(0, getTextLength());
}
 
Example 17
Source File: SQFString.java    From arma-intellij-plugin with MIT License 4 votes vote down vote up
/**
 * @return TextRange that doesn't include the quotes and the range is relative to the <b>element</b>
 * @see #getNonQuoteRangeRelativeToFile()
 */
@NotNull
public TextRange getNonQuoteRangeRelativeToElement() {
	return TextRange.from(1, getTextLength() - 2);
}
 
Example 18
Source File: SQFVariableReference.java    From arma-intellij-plugin with MIT License 4 votes vote down vote up
@Override
public TextRange getRangeInElement() {
	return TextRange.from(0, variable.getTextLength());
}
 
Example 19
Source File: BashEvalElementType.java    From BashSupport with Apache License 2.0 4 votes vote down vote up
@Override
protected ASTNode doParseContents(@NotNull ASTNode chameleon, @NotNull PsiElement psi) {
    Project project = psi.getProject();
    boolean supportEvalEscapes = BashProjectSettings.storedSettings(project).isEvalEscapesEnabled();

    String originalText = chameleon.getChars().toString();
    ParserDefinition def = LanguageParserDefinitions.INSTANCE.forLanguage(BashFileType.BASH_LANGUAGE);

    boolean isDoubleQuoted = originalText.startsWith("\"") && originalText.endsWith("\"");
    boolean isSingleQuoted = originalText.startsWith("'") && originalText.endsWith("'");
    boolean isEscapingSingleQuoted = originalText.startsWith("$'") && originalText.endsWith("'");
    boolean isUnquoted = !isDoubleQuoted && !isSingleQuoted && !isEscapingSingleQuoted;

    String prefix = isUnquoted ? "" : originalText.subSequence(0, isEscapingSingleQuoted ? 2 : 1).toString();
    String content = isUnquoted ? originalText : originalText.subSequence(isEscapingSingleQuoted ? 2 : 1, originalText.length() - 1).toString();
    String suffix = isUnquoted ? "" : originalText.subSequence(originalText.length() - 1, originalText.length()).toString();

    TextPreprocessor textProcessor;
    if (supportEvalEscapes) {
        if (isEscapingSingleQuoted) {
            textProcessor = new BashEnhancedTextPreprocessor(TextRange.from(prefix.length(), content.length()));
        } else if (isSingleQuoted) {
            //no escape handling for single-quoted strings
            textProcessor = new BashIdentityTextPreprocessor(TextRange.from(prefix.length(), content.length()));
        } else {
            //fallback to simple escape handling
            textProcessor = new BashSimpleTextPreprocessor(TextRange.from(prefix.length(), content.length()));
        }
    } else {
        textProcessor = new BashIdentityTextPreprocessor(TextRange.from(prefix.length(), content.length()));
    }

    StringBuilder unescapedContent = new StringBuilder(content.length());
    textProcessor.decode(content, unescapedContent);

    Lexer lexer = isUnquoted
            ? def.createLexer(project)
            : new PrefixSuffixAddingLexer(def.createLexer(project), prefix, TokenType.WHITE_SPACE, suffix, TokenType.WHITE_SPACE);

    PsiBuilder psiBuilder = new UnescapingPsiBuilder(project,
            def,
            lexer,
            chameleon,
            originalText,
            prefix + unescapedContent + suffix,
            textProcessor);

    return def.createParser(project).parse(this, psiBuilder).getFirstChildNode();
}
 
Example 20
Source File: HaskellVaridBaseImpl.java    From intellij-haskforce with Apache License 2.0 4 votes vote down vote up
@Override
@NotNull
public PsiReference getReference() {
  String s = getName();
  return new HaskellReference(this, TextRange.from(0, s.length()));
}