当前位置: 首页>>代码示例>>Java>>正文


Java Document.getCharsSequence方法代码示例

本文整理汇总了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);
    }
 
开发者ID:reasonml-editor,项目名称:reasonml-idea-plugin,代码行数:61,代码来源:RmlEditorLinePainter.java

示例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;
}
 
开发者ID:xylo,项目名称:intellij-postfix-templates,代码行数:63,代码来源:CustomJavaStringPostfixTemplate.java

示例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;
}
 
开发者ID:xylo,项目名称:intellij-postfix-templates,代码行数:54,代码来源:CustomKotlinStringPostfixTemplate.java


注:本文中的com.intellij.openapi.editor.Document.getCharsSequence方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。