本文整理汇总了Java中com.intellij.openapi.editor.Document.getCharsSequence方法的典型用法代码示例。如果您正苦于以下问题:Java Document.getCharsSequence方法的具体用法?Java Document.getCharsSequence怎么用?Java Document.getCharsSequence使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.editor.Document
的用法示例。
在下文中一共展示了Document.getCharsSequence方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getLineExtensions
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
@Override
public Collection<LineExtensionInfo> getLineExtensions(@NotNull Project project, @NotNull VirtualFile file, int lineNumber) {
final Document document = FileDocumentManager.getInstance().getDocument(file);
if (document == null) {
return null;
}
Editor selectedTextEditor = FileEditorManager.getInstance(project).getSelectedTextEditor();
if (selectedTextEditor == null) {
return null;
}
PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
LineNumbering lineNumbering = new LineNumbering(document.getCharsSequence());
Collection<PsiLet> letStatements = PsiTreeUtil.findChildrenOfType(psiFile, PsiLet.class);
String inferredType = null;
for (PsiLet letStatement : letStatements) {
int letOffset = letStatement.getTextOffset();
// TODO: I'm using the LineNumbering class to avoid frequent exceptions about read access,
// but I would prefer to use runReadAction method.
MerlinPosition letPosition = lineNumbering.offsetToPosition(letOffset);
// LogicalPosition letPosition = new LogicalPosition;
// letPosition[0] = selectedTextEditor.offsetToLogicalPosition(letOffset);
if (letPosition.line - 1 == lineNumber) {
inferredType = letStatement.getInferredType();
break;
}
}
/*
Function<PsiLet, String> findInferredType = letStatement -> {
// Found a let statement, try to get its type if in correct line number
final int[] letOffset = new int[]{-1};
// ApplicationManager.getApplication().runReadAction(() -> { // Freezing pb ?
letOffset[0] = letStatement.getTextOffset();
// });
// TODO: I'm using the LineNumbering class to avoid frequent exceptions about read access,
// but I would prefer to use runReadAction method.
MerlinPosition letPosition = lineNumbering.offsetToPosition(letOffset[0]);
// LogicalPosition letPosition = new LogicalPosition;
// ApplicationManager.getApplication().runReadAction(() -> {
// letPosition[0] = selectedTextEditor.offsetToLogicalPosition(letOffset);
// });
return letPosition.line - 1 == lineNumber ? letStatement.getInferredType() : null;
};
String inferredType;
inferredType = letStatements.parallelStream().map(findInferredType).filter(Objects::nonNull).findFirst().orElse(null);
*/
if (inferredType == null) {
return null;
}
final TextAttributes attributes = getNormalAttributes();
LineExtensionInfo info = new LineExtensionInfo(" " + inferredType, attributes);
return Collections.singletonList(info);
}
示例2: collectExpressions
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
public static List<PsiExpression> collectExpressions(final PsiFile file,
final Document document,
final int offset,
boolean acceptVoid) {
CharSequence text = document.getCharsSequence();
int correctedOffset = offset;
int textLength = document.getTextLength();
if (offset >= textLength) {
correctedOffset = textLength - 1;
} else if (!Character.isJavaIdentifierPart(text.charAt(offset))) {
correctedOffset--;
}
if (correctedOffset < 0) {
correctedOffset = offset;
} else if (!Character.isJavaIdentifierPart(text.charAt(correctedOffset))) {
if (text.charAt(correctedOffset) == ';') {//initially caret on the end of line
correctedOffset--;
}
if (correctedOffset < 0 || text.charAt(correctedOffset) != ')') {
correctedOffset = offset;
}
}
final PsiElement elementAtCaret = file.findElementAt(correctedOffset);
final List<PsiExpression> expressions = new ArrayList<>();
/*for (PsiElement element : statementsInRange) {
if (element instanceof PsiExpressionStatement) {
final PsiExpression expression = ((PsiExpressionStatement)element).getExpression();
if (expression.getType() != PsiType.VOID) {
expressions.add(expression);
}
}
}*/
PsiExpression expression = PsiTreeUtil.getParentOfType(elementAtCaret, PsiExpression.class);
while (expression != null) {
if (!expressions.contains(expression) && !(expression instanceof PsiParenthesizedExpression) && !(expression instanceof PsiSuperExpression) &&
(acceptVoid || !PsiType.VOID.equals(expression.getType()))) {
if (expression instanceof PsiMethodReferenceExpression) {
expressions.add(expression);
} else if (!(expression instanceof PsiAssignmentExpression)) {
if (!(expression instanceof PsiReferenceExpression)) {
expressions.add(expression);
} else {
if (!(expression.getParent() instanceof PsiMethodCallExpression)) {
final PsiElement resolve = ((PsiReferenceExpression) expression).resolve();
if (!(resolve instanceof PsiClass) && !(resolve instanceof PsiPackage)) {
expressions.add(expression);
}
}
}
}
}
expression = PsiTreeUtil.getParentOfType(expression, PsiExpression.class);
}
/*
for (PsiElement psiElement : expressions) {
System.out.println("parent: " + psiElement + " at " + psiElement.getTextRange());
}
*/
return expressions;
}
示例3: collectExpressions
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
public static List<PsiElement> collectExpressions(final PsiFile file,
final Document document,
final int offset,
boolean acceptVoid) {
CharSequence text = document.getCharsSequence();
int correctedOffset = offset;
int textLength = document.getTextLength();
if (offset >= textLength) {
correctedOffset = textLength - 1;
} else if (!Character.isJavaIdentifierPart(text.charAt(offset))) {
correctedOffset--;
}
if (correctedOffset < 0) {
correctedOffset = offset;
} else if (!Character.isJavaIdentifierPart(text.charAt(correctedOffset))) {
if (text.charAt(correctedOffset) == ';') {//initially caret on the end of line
correctedOffset--;
}
if (correctedOffset < 0 || text.charAt(correctedOffset) != ')') {
correctedOffset = offset;
}
}
final PsiElement elementAtCaret = file.findElementAt(correctedOffset);
final List<PsiElement> expressions = new ArrayList<>();
PsiElement expression = PsiTreeUtil.getParentOfType(elementAtCaret, PsiElement.class);
while (expression != null && expression.getTextRange().getEndOffset() == elementAtCaret.getTextRange().getEndOffset()) {
//System.out.println(expression + " - " + expression.getText() + " - " + expression.getTextRange());
final PsiElement finalExpression = expression;
if (expression.getPrevSibling() == null || expression.getPrevSibling().getNode().getElementType() == TokenType.WHITE_SPACE) {
if (expressions.stream().noneMatch(pe -> finalExpression.getTextRange().equals(pe.getTextRange()))) {
expressions.add(expression);
}
} else {
//System.out.println("prevSilbing: " + expression.getPrevSibling().getNode().getElementType());
}
//expression = PsiTreeUtil.getParentOfType(expression, KtExpression.class);
expression = expression.getParent();
}
// TODO: For an unknown reason this code completion works only with a single expression and not with multiple ones.
// TODO: Therefore we have to cut our list to a singleton list.
if (expressions.isEmpty()) {
return expressions;
}
ArrayList<PsiElement> es = new ArrayList<>();
es.add(expressions.get(0));
//es.add(expressions.get(expressions.size()-1));
return es;
}