本文整理匯總了Java中com.intellij.openapi.editor.SelectionModel.setSelection方法的典型用法代碼示例。如果您正苦於以下問題:Java SelectionModel.setSelection方法的具體用法?Java SelectionModel.setSelection怎麽用?Java SelectionModel.setSelection使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.intellij.openapi.editor.SelectionModel
的用法示例。
在下文中一共展示了SelectionModel.setSelection方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: invoke
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
@Override
public void invoke(@NotNull final Project project, final Editor editor, final PsiFile file, @Nullable final DataContext dataContext) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
final int offset = editor.getCaretModel().getOffset();
final List<GrExpression> expressions = collectExpressions(file, editor, offset, false);
if (expressions.isEmpty()) {
updateSelectionForVariable(editor, file, selectionModel, offset);
}
else if (expressions.size() == 1) {
final TextRange textRange = expressions.get(0).getTextRange();
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
}
else {
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {
@Override
public void pass(final GrExpression selectedValue) {
invoke(project, editor, file, selectedValue.getTextRange().getStartOffset(), selectedValue.getTextRange().getEndOffset());
}
}, GR_EXPRESSION_RENDERER);
return;
}
}
invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}
示例2: wrap
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
/**
* Wraps selection.
*
* @param editor Current editor.
*/
private void wrap(@NotNull TextEditor editor) {
final Document document = editor.getEditor().getDocument();
final SelectionModel selectionModel = editor.getEditor().getSelectionModel();
final CaretModel caretModel = editor.getEditor().getCaretModel();
final int start = selectionModel.getSelectionStart();
final int end = selectionModel.getSelectionEnd();
final String text = StringUtil.notNullize(selectionModel.getSelectedText());
String newText = getLeftText() + text + getRightText();
int newStart = start + getLeftText().length();
int newEnd = StringUtil.isEmpty(text) ? newStart : end + getLeftText().length();
document.replaceString(start, end, newText);
selectionModel.setSelection(newStart, newEnd);
caretModel.moveToOffset(newEnd);
}
示例3: unwrap
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
/**
* Unwraps selection.
*
* @param editor Current editor.
* @param matched Matched PSI element.
*/
private void unwrap(@NotNull final TextEditor editor, @NotNull final PsiElement matched) {
final Document document = editor.getEditor().getDocument();
final SelectionModel selectionModel = editor.getEditor().getSelectionModel();
final CaretModel caretModel = editor.getEditor().getCaretModel();
final int start = matched.getTextRange().getStartOffset();
final int end = matched.getTextRange().getEndOffset();
final String text = StringUtil.notNullize(matched.getText());
String newText = StringUtil.trimEnd(StringUtil.trimStart(text, getLeftText()), getRightText());
int newStart = selectionModel.getSelectionStart() - getLeftText().length();
int newEnd = selectionModel.getSelectionEnd() - getLeftText().length();
document.replaceString(start, end, newText);
selectionModel.setSelection(newStart, newEnd);
caretModel.moveToOffset(newEnd);
}
示例4: propertiesHandling
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
private boolean propertiesHandling(Editor editor, DataContext dataContext, SelectionModel selectionModel,
PsiFile psiFile) {
PsiElement elementAtCaret = PsiUtilBase.getElementAtCaret(editor);
if (elementAtCaret instanceof PsiWhiteSpace) {
return false;
} else if (elementAtCaret instanceof LeafPsiElement) {
IElementType elementType = ((LeafPsiElement) elementAtCaret).getElementType();
if (elementType.toString().equals("Properties:VALUE_CHARACTERS")
|| elementType.toString().equals("Properties:KEY_CHARACTERS")) {
TextRange textRange = elementAtCaret.getTextRange();
if (textRange.getLength() == 0) {
return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
}
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
return true;
}
}
return false;
}
示例5: invoke
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
@Override
public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
final TemplateImpl template = getTemplate();
SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
final PsiExpression iterableExpression = getIterableExpression(editor, file);
LOG.assertTrue(iterableExpression != null);
TextRange textRange = iterableExpression.getTextRange();
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
}
new InvokeTemplateAction(template, editor, project, new HashSet<Character>()).perform();
}
示例6: setTextToEditor
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
protected void setTextToEditor(String text) {
myEditor.getMarkupModel().removeAllHighlighters();
myEditor.getDocument().setText(text);
myEditor.getHighlighter().setText(text);
if (myTextAttributes != null) {
myEditor.getMarkupModel().addRangeHighlighter(0, myEditor.getDocument().getTextLength(),
HighlighterLayer.ADDITIONAL_SYNTAX, myTextAttributes, HighlighterTargetArea.EXACT_RANGE);
}
((EditorImpl)myEditor).resetSizes();
((EditorImpl)myEditor).setPaintSelection(mySelected);
SelectionModel selectionModel = myEditor.getSelectionModel();
selectionModel.setSelection(0, mySelected ? myEditor.getDocument().getTextLength() : 0);
}
示例7: invoke
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
@Override
public void invoke(@NotNull final Project project, @NotNull final Editor editor, @NotNull final PsiFile file, @Nullable final DataContext dataContext) {
final SelectionModel selectionModel = editor.getSelectionModel();
if (!selectionModel.hasSelection()) {
final int offset = editor.getCaretModel().getOffset();
final List<GrExpression> expressions = GrIntroduceHandlerBase.collectExpressions(file, editor, offset, false);
if (expressions.isEmpty()) {
GrIntroduceHandlerBase.updateSelectionForVariable(editor, file, selectionModel, offset);
}
else if (expressions.size() == 1 || ApplicationManager.getApplication().isUnitTestMode()) {
final TextRange textRange = expressions.get(0).getTextRange();
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
}
else {
IntroduceTargetChooser.showChooser(editor, expressions, new Pass<GrExpression>() {
@Override
public void pass(final GrExpression selectedValue) {
invoke(project, editor, file, selectedValue.getTextRange().getStartOffset(), selectedValue.getTextRange().getEndOffset());
}
}, new Function<GrExpression, String>() {
@Override
public String fun(GrExpression grExpression) {
return grExpression.getText();
}
}
);
return;
}
}
invoke(project, editor, file, selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}
示例8: updateSelectionForVariable
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
public static void updateSelectionForVariable(Editor editor, PsiFile file, SelectionModel selectionModel, int offset) {
final GrVariable variable = findVariableAtCaret(file, editor, offset);
if (variable == null || variable instanceof GrField || variable instanceof GrParameter) {
selectionModel.selectLineAtCaret();
}
else {
final TextRange textRange = variable.getTextRange();
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
}
}
示例9: genericHandling
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
private boolean genericHandling(Editor editor, DataContext dataContext, SelectionModel selectionModel,
PsiFile psiFile) {
int caretOffset = editor.getCaretModel().getOffset();
PsiElement elementAtCaret = PsiUtilBase.getElementAtCaret(editor);
if (elementAtCaret instanceof PsiPlainText) {
return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
} else if (elementAtCaret instanceof PsiWhiteSpace) {
elementAtCaret = PsiUtilBase.getElementAtOffset(psiFile, caretOffset - 1);
}
if (elementAtCaret == null || elementAtCaret instanceof PsiWhiteSpace) {
return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
} else {
TextRange textRange = elementAtCaret.getTextRange();
if (textRange.getLength() == 0) {
return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
}
selectionModel.setSelection(textRange.getStartOffset(), textRange.getEndOffset());
String selectedText = selectionModel.getSelectedText();
if (selectedText != null && selectedText.contains("\n")) {
selectionModel.removeSelection();
return super.selectSomethingUnderCaret(editor, dataContext, selectionModel);
}
if (Style.isQuoted(selectedText)) {
selectionModel.setSelection(selectionModel.getSelectionStart() + 1,
selectionModel.getSelectionEnd() - 1);
}
if (caretOffset < selectionModel.getSelectionStart()) {
editor.getCaretModel().moveToOffset(selectionModel.getSelectionStart());
}
if (caretOffset > selectionModel.getSelectionEnd()) {
editor.getCaretModel().moveToOffset(selectionModel.getSelectionEnd());
}
return true;
}
}
示例10: selectSomethingUnderCaret
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
protected boolean selectSomethingUnderCaret(Editor editor, DataContext dataContext, SelectionModel selectionModel) {
selectionModel.selectLineAtCaret();
String selectedText = selectionModel.getSelectedText();
if (selectedText != null && selectedText.endsWith("\n")) {
selectionModel.setSelection(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd() - 1);
}
return true;
}
示例11: setTextToEditor
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
protected void setTextToEditor(String text) {
myEditor.getMarkupModel().removeAllHighlighters();
myEditor.getDocument().setText(text);
((EditorImpl)myEditor).resetSizes();
myEditor.getHighlighter().setText(text);
if (myTextAttributes != null) {
myEditor.getMarkupModel().addRangeHighlighter(0, myEditor.getDocument().getTextLength(), HighlighterLayer.ADDITIONAL_SYNTAX, myTextAttributes, HighlighterTargetArea.EXACT_RANGE);
}
((EditorImpl)myEditor).setPaintSelection(mySelected);
SelectionModel selectionModel = myEditor.getSelectionModel();
selectionModel.setSelection(0, mySelected ? myEditor.getDocument().getTextLength() : 0);
}
示例12: restoreCaretAndSelection
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
private static void restoreCaretAndSelection(@NotNull final PsiFile file, @NotNull final Editor editor, boolean selectionStartAtCaret,
boolean hasSelection, @NotNull final SelectionContainer selectionContainer, int shift,
int offset, @NotNull final MyLineRange toMove) {
final Document document = editor.getDocument();
final SelectionModel selectionModel = editor.getSelectionModel();
final CaretModel caretModel = editor.getCaretModel();
Integer selectionLen = selectionContainer.myLen;
final PsiElement at = file.findElementAt(offset);
if (at != null) {
final PsiElement added = getCommentOrStatement(document, at);
int size = toMove.size;
if (size > 1) {
PsiElement tmp = added.getNextSibling();
while (size > 1 && tmp != null) {
if (tmp instanceof PsiWhiteSpace) {
if (!selectionStartAtCaret)
shift += tmp.getTextLength();
selectionLen += tmp.getTextLength();
}
tmp = tmp.getNextSibling();
size -= 1;
}
}
if (shift < 0) shift = 0;
final int column = editor.offsetToLogicalPosition(added.getTextRange().getStartOffset()).column;
if (selectionContainer.myAtTheBeginning || column < selectionContainer.myAdditional) {
selectionLen += column;
}
else {
selectionLen += selectionContainer.myAdditional;
}
if (selectionContainer.myAtTheBeginning && selectionStartAtCaret)
shift = -column;
}
final int documentLength = document.getTextLength();
int newCaretOffset = offset + shift;
if (newCaretOffset >= documentLength) newCaretOffset = documentLength;
caretModel.moveToOffset(newCaretOffset);
if (hasSelection) {
if (selectionStartAtCaret) {
int newSelectionEnd = newCaretOffset + selectionLen;
selectionModel.setSelection(newCaretOffset, newSelectionEnd);
}
else {
int newSelectionStart = newCaretOffset - selectionLen;
selectionModel.setSelection(newSelectionStart, newCaretOffset);
}
}
}
示例13: executeWriteAction
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
@Override
public void executeWriteAction(Editor editor, DataContext dataContext) {
final SelectionModel selectionModel = editor.getSelectionModel();
final int[] starts;
final int[] ends;
LogicalPosition blockStart = null;
LogicalPosition blockEnd = null;
if (selectionModel.hasBlockSelection()) {
starts = selectionModel.getBlockSelectionStarts();
ends = selectionModel.getBlockSelectionEnds();
blockStart = selectionModel.getBlockStart();
blockEnd = selectionModel.getBlockEnd();
}
else {
if (!selectionModel.hasSelection()) {
selectionModel.selectWordAtCaret(true);
}
starts = new int[] {selectionModel.getSelectionStart()};
ends = new int[] {selectionModel.getSelectionEnd()};
}
selectionModel.removeBlockSelection();
selectionModel.removeSelection();
for (int i = 0; i < starts.length; i++) {
int startOffset = starts[i];
int endOffset = ends[i];
StringBuilder builder = new StringBuilder();
final String text = editor.getDocument().getCharsSequence().subSequence(startOffset, endOffset).toString();
toCase(builder, text, true);
if (text.equals(builder.toString())) {
toCase(builder, text, false);
}
editor.getDocument().replaceString(startOffset, endOffset, builder.toString());
}
if (blockStart != null) {
selectionModel.setBlockSelection(blockStart, blockEnd);
}
else {
selectionModel.setSelection(starts[0], ends[0]);
}
}
示例14: actionPerformed
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
@Override
public void actionPerformed(AnActionEvent e) {
PsiElement el = MyActionUtils.getSelectedPsiElement(e);
if ( el==null ) return;
final PsiFile psiFile = e.getData(LangDataKeys.PSI_FILE);
if (psiFile == null) return;
Editor editor = e.getData(PlatformDataKeys.EDITOR);
if ( editor==null ) return;
final Document doc = editor.getDocument();
SelectionModel selectionModel = editor.getSelectionModel();
String grammarText = psiFile.getText();
ParsingResult results = ParsingUtils.parseANTLRGrammar(grammarText);
final Parser parser = results.parser;
final ParserRuleContext tree = (ParserRuleContext)results.tree;
TokenStream tokens = parser.getTokenStream();
int selStart = selectionModel.getSelectionStart();
int selStop = selectionModel.getSelectionEnd()-1; // I'm inclusive and they are exclusive for end offset
// find appropriate tokens for bounds, don't include WS
Token start = RefactorUtils.getTokenForCharIndex(tokens, selStart);
Token stop = RefactorUtils.getTokenForCharIndex(tokens, selStop);
if ( start==null || stop==null ) {
return;
}
if ( start.getType()==ANTLRv4Lexer.WS ) {
start = tokens.get(start.getTokenIndex()+1);
}
if ( stop.getType()==ANTLRv4Lexer.WS ) {
stop = tokens.get(stop.getTokenIndex()-1);
}
selectionModel.setSelection(start.getStartIndex(), stop.getStopIndex() + 1);
final Project project = e.getProject();
final ChooseExtractedRuleName nameChooser = new ChooseExtractedRuleName(project);
nameChooser.show();
if ( nameChooser.ruleName==null ) return;
// make new rule string
final String ruleText = selectionModel.getSelectedText();
final int insertionPoint = RefactorUtils.getCharIndexOfNextRuleStart(tree, start.getTokenIndex());
final String newRule = "\n"+nameChooser.ruleName + " : " + ruleText + " ;" + "\n";
final Token start_ = start;
final Token stop_ = stop;
WriteCommandAction setTextAction = new WriteCommandAction(project) {
@Override
protected void run(final Result result) throws Throwable {
// do all as one operation.
if ( insertionPoint>=doc.getTextLength() ) {
doc.insertString(doc.getTextLength(), newRule);
}
else {
doc.insertString(insertionPoint, newRule);
}
doc.replaceString(start_.getStartIndex(), stop_.getStopIndex()+1, nameChooser.ruleName);
}
};
setTextAction.execute();
// TODO: only allow selection of fully-formed syntactic entity.
// E.g., "A (',' A" is invalid grammatically as a rule.
}
示例15: selectText
import com.intellij.openapi.editor.SelectionModel; //導入方法依賴的package包/類
@Override
protected void selectText(Editor editor) {
SelectionModel selectionModel = editor.getSelectionModel();
selectionModel.selectLineAtCaret();
selectionModel.setSelection(selectionModel.getSelectionStart(), selectionModel.getSelectionEnd());
}