本文整理汇总了Java中com.intellij.openapi.editor.Document.getText方法的典型用法代码示例。如果您正苦于以下问题:Java Document.getText方法的具体用法?Java Document.getText怎么用?Java Document.getText使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.openapi.editor.Document
的用法示例。
在下文中一共展示了Document.getText方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleEnterInComment
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
private static void handleEnterInComment(
PsiElement element, @NotNull PsiFile file, @NotNull Editor editor) {
if (element.getText().startsWith("/*")) {
Document document = editor.getDocument();
int caretOffset = editor.getCaretModel().getOffset();
int lineNumber = document.getLineNumber(caretOffset);
String lineTextBeforeCaret =
document.getText(new TextRange(document.getLineStartOffset(lineNumber), caretOffset));
String lineTextAfterCaret =
document.getText(new TextRange(caretOffset, document.getLineEndOffset(lineNumber)));
if (lineTextAfterCaret.equals("*/")) {
return;
}
String toInsert = lineTextBeforeCaret.equals("") ? " * " : "* ";
insertText(file, editor, toInsert, toInsert.length());
}
}
示例2: postProcessEnter
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
@Override
public Result postProcessEnter(
@NotNull PsiFile file, @NotNull Editor editor, @NotNull DataContext dataContext) {
if (file.getFileType() != SoyFileType.INSTANCE) {
return Result.Continue;
}
int caretOffset = editor.getCaretModel().getOffset();
PsiElement element = file.findElementAt(caretOffset);
Document document = editor.getDocument();
int lineNumber = document.getLineNumber(caretOffset) - 1;
int lineStartOffset = document.getLineStartOffset(lineNumber);
String lineTextBeforeCaret = document.getText(new TextRange(lineStartOffset, caretOffset));
if (element instanceof PsiComment && element.getTextOffset() < caretOffset) {
handleEnterInComment(element, file, editor);
} else if (lineTextBeforeCaret.startsWith("/*")) {
insertText(file, editor, " * \n ", 3);
}
return Result.Continue;
}
示例3: refmt
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
void refmt(Project project, String format, Document document) {
String oldText = document.getText();
String newText = m_refmtProcess.run(project, format, oldText);
if (!oldText.isEmpty() && !newText.isEmpty()) { // additional protection
document.setText(newText);
}
}
示例4: getSelectedText
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
/**
* Returns the selected text if there is any selection. If not return all based on parameter
*
* @param defaultToAll If no selection, then this decides whether to return all text
*/
String getSelectedText(boolean defaultToAll) {
ConsoleView console = this.getConsole();
Editor myEditor = console != null ? (Editor) CommonDataKeys.EDITOR.getData((DataProvider) console) : null;
if (myEditor != null) {
Document document = myEditor.getDocument();
final SelectionModel selectionModel = myEditor.getSelectionModel();
if (selectionModel.hasSelection()) {
return selectionModel.getSelectedText();
} else if (defaultToAll) {
return document.getText();
}
}
return null;
}
示例5: getCurrentWords
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
private String getCurrentWords(Editor editor) {
Document document = editor.getDocument();
CaretModel caretModel = editor.getCaretModel();
int caretOffset = caretModel.getOffset();
int lineNum = document.getLineNumber(caretOffset);
int lineStartOffset = document.getLineStartOffset(lineNum);
int lineEndOffset = document.getLineEndOffset(lineNum);
String lineContent = document.getText(new TextRange(lineStartOffset, lineEndOffset));
char[] chars = lineContent.toCharArray();
int start = 0, end = 0, cursor = caretOffset - lineStartOffset;
if (!Character.isLetter(chars[cursor])) {
return null;
}
for (int ptr = cursor; ptr >= 0; ptr--) {
if (!Character.isLetter(chars[ptr])) {
start = ptr + 1;
break;
}
}
int lastLetter = 0;
for (int ptr = cursor; ptr < lineEndOffset - lineStartOffset; ptr++) {
lastLetter = ptr;
if (!Character.isLetter(chars[ptr])) {
end = ptr;
break;
}
}
if (end == 0) {
end = lastLetter + 1;
}
return new String(chars, start, end - start);
}
示例6: rememberAnswers
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
private void rememberAnswers(int nextSubtaskIndex, @NotNull TaskWithSubtasks task) {
VirtualFile taskDir = task.getTaskDir(myProject);
if (taskDir == null) {
return;
}
VirtualFile srcDir = taskDir.findChild(EduNames.SRC);
if (srcDir != null) {
taskDir = srcDir;
}
for (Map.Entry<String, TaskFile> entry : task.getTaskFiles().entrySet()) {
TaskFile taskFile = entry.getValue();
VirtualFile virtualFile = taskDir.findFileByRelativePath(entry.getKey());
if (virtualFile == null) {
continue;
}
Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
if (document == null) {
continue;
}
for (AnswerPlaceholder placeholder : taskFile.getActivePlaceholders()) {
if (placeholder.getSubtaskInfos().containsKey(nextSubtaskIndex - 1)) {
int offset = placeholder.getOffset();
String answer = document.getText(TextRange.create(offset, offset + placeholder.getRealLength()));
placeholder.getSubtaskInfos().get(nextSubtaskIndex - 1).setAnswer(answer);
}
}
}
}
示例7: replaceAnswerPlaceholder
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
private static void replaceAnswerPlaceholder(@NotNull final Document document,
@NotNull final AnswerPlaceholder placeholder) {
final int offset = placeholder.getOffset();
final String text = document.getText(TextRange.create(offset, offset + placeholder.getRealLength()));
placeholder.setTaskText(text);
placeholder.init();
String replacementText = placeholder.getPossibleAnswer();
CommandProcessor.getInstance().runUndoTransparentAction(() -> ApplicationManager.getApplication().runWriteAction(() -> {
document.replaceString(offset, offset + placeholder.getRealLength(), replacementText);
FileDocumentManager.getInstance().saveDocument(document);
}));
}
示例8: getCurrentWords
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
public String getCurrentWords(Editor editor) {
Document document = editor.getDocument();
CaretModel caretModel = editor.getCaretModel();
int caretOffset = caretModel.getOffset();
int lineNum = document.getLineNumber(caretOffset);
int lineStartOffset = document.getLineStartOffset(lineNum);
int lineEndOffset = document.getLineEndOffset(lineNum);
String lineContent = document.getText(new TextRange(lineStartOffset, lineEndOffset));
char[] chars = lineContent.toCharArray();
int start = 0, end = 0, cursor = caretOffset - lineStartOffset;
if (!Character.isLetter(chars[cursor])) {
Logger.warn("Caret not in a word");
return null;
}
for (int ptr = cursor; ptr >= 0; ptr--) {
if (!Character.isLetter(chars[ptr])) {
start = ptr + 1;
break;
}
}
int lastLetter = 0;
for (int ptr = cursor; ptr < lineEndOffset - lineStartOffset; ptr++) {
lastLetter = ptr;
if (!Character.isLetter(chars[ptr])) {
end = ptr;
break;
}
}
if (end == 0) {
end = lastLetter + 1;
}
String ret = new String(chars, start, end-start);
Logger.info("Selected words: " + ret);
return ret;
}
示例9: applyFix
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
try {
PsiElement element = descriptor.getPsiElement();
Document document = PsiDocumentManager.getInstance(project).getDocument(element.getContainingFile());
String text = document.getText();
document.setText(text.substring(0, element.getTextOffset()) + CsvCodeStyleSettings.getCurrentSeparator(project) + text.substring(element.getTextOffset()));
} catch (IncorrectOperationException e) {
LOG.error(e);
}
}
示例10: getPreviousChar
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
private String getPreviousChar(Document document, int offset) {
return offset <= 0 ? " " : document.getText(new TextRange(offset - 1, offset));
}
示例11: getNextChar
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
private String getNextChar(Document document, int offset) {
return offset >= document.getTextLength()
? " "
: document.getText(new TextRange(offset, offset + 1));
}
示例12: smartCheck
import com.intellij.openapi.editor.Document; //导入方法依赖的package包/类
private static void smartCheck(@NotNull final AnswerPlaceholder placeholder,
@NotNull final Project project,
@NotNull final VirtualFile answerFile,
@NotNull final TaskFile answerTaskFile,
@NotNull final TaskFile usersTaskFile,
@NotNull final PyStudyTestRunner testRunner,
@NotNull final VirtualFile virtualFile,
@NotNull final Document usersDocument) {
VirtualFile fileWindows = null;
VirtualFile windowCopy = null;
try {
final int index = placeholder.getIndex();
String windowCopyName = answerFile.getNameWithoutExtension() + index + EduNames.WINDOW_POSTFIX + answerFile.getExtension();
windowCopy = answerFile.copy(project, answerFile.getParent(), windowCopyName);
final FileDocumentManager documentManager = FileDocumentManager.getInstance();
final Document windowDocument = documentManager.getDocument(windowCopy);
if (windowDocument != null) {
TaskFile windowTaskFile = answerTaskFile.getTask().copy().getTaskFile(StudyUtils.pathRelativeToTask(virtualFile));
if (windowTaskFile == null) {
return;
}
EduDocumentListener listener = new EduDocumentListener(windowTaskFile);
windowDocument.addDocumentListener(listener);
int start = placeholder.getOffset();
int end = start + placeholder.getRealLength();
final AnswerPlaceholder userAnswerPlaceholder = usersTaskFile.getAnswerPlaceholders().get(placeholder.getIndex());
int userStart = userAnswerPlaceholder.getOffset();
int userEnd = userStart + userAnswerPlaceholder.getRealLength();
String text = usersDocument.getText(new TextRange(userStart, userEnd));
windowDocument.replaceString(start, end, text);
ApplicationManager.getApplication().runWriteAction(() -> documentManager.saveDocument(windowDocument));
fileWindows = EduUtils.flushWindows(windowTaskFile, windowCopy);
Process smartTestProcess = testRunner.createCheckProcess(project, windowCopy.getPath());
final CapturingProcessHandler handler = new CapturingProcessHandler(smartTestProcess, null, windowCopy.getPath());
final ProcessOutput output = handler.runProcess();
final Course course = StudyTaskManager.getInstance(project).getCourse();
if (course != null) {
boolean res = StudyTestsOutputParser.getTestsOutput(output, course.isAdaptive()).isSuccess();
StudyTaskManager.getInstance(project).setStatus(userAnswerPlaceholder, res ? StudyStatus.Solved : StudyStatus.Failed);
}
}
}
catch (ExecutionException | IOException e) {
LOG.error(e);
}
finally {
StudyUtils.deleteFile(windowCopy);
StudyUtils.deleteFile(fileWindows);
}
}