Java Code Examples for com.intellij.psi.util.PsiUtilBase#getElementAtCaret()

The following examples show how to use com.intellij.psi.util.PsiUtilBase#getElementAtCaret() . 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: AbstractCaseConvertingAction.java    From StringManipulation with Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 2
Source File: AbstractCaseConvertingAction.java    From StringManipulation with Apache License 2.0 5 votes vote down vote up
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 (StringUtil.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;
	}
}