本文整理汇总了Java中com.intellij.psi.util.PsiUtilCore.getElementAtOffset方法的典型用法代码示例。如果您正苦于以下问题:Java PsiUtilCore.getElementAtOffset方法的具体用法?Java PsiUtilCore.getElementAtOffset怎么用?Java PsiUtilCore.getElementAtOffset使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.intellij.psi.util.PsiUtilCore
的用法示例。
在下文中一共展示了PsiUtilCore.getElementAtOffset方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: handleInsert
import com.intellij.psi.util.PsiUtilCore; //导入方法依赖的package包/类
@Override
public void handleInsert(InsertionContext context, LookupElement item) {
final Object object = item.getObject();
if (object instanceof PsiMethod) {
final PsiElement newElement = PsiUtilCore.getElementAtOffset(context.getFile(), context.getStartOffset());
final int start = newElement.getTextRange().getEndOffset();
final PsiElement params = newElement.getParent().getParent();
final int end = params.getTextRange().getEndOffset() - 1;
final String types = getMethodTypes((PsiMethod)object);
context.getDocument().replaceString(start, end, types);
context.commitDocument();
final PsiElement firstParam = PsiUtilCore.getElementAtOffset(context.getFile(), context.getStartOffset());
final PsiMethodCallExpression methodCall = PsiTreeUtil.getParentOfType(firstParam, PsiMethodCallExpression.class);
if (methodCall != null) {
JavaCodeStyleManager.getInstance(context.getProject()).shortenClassReferences(methodCall);
}
}
}
示例2: getContext
import com.intellij.psi.util.PsiUtilCore; //导入方法依赖的package包/类
@NotNull
public static PsiElement getContext(@NotNull PsiFile file, int offset, boolean searchInInjectedFragment) {
PsiElement element = null;
if (searchInInjectedFragment && !InjectedLanguageManager.getInstance(file.getProject()).isInjectedFragment(file)) {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject());
Document document = documentManager.getDocument(file);
if (document != null && !documentManager.isCommitted(document)) {
LOGGER.error("Trying to access to injected template context on uncommited document, offset = " + offset,
AttachmentFactory.createAttachment(file.getVirtualFile()));
}
else {
element = InjectedLanguageUtil.findInjectedElementNoCommit(file, offset);
}
}
if (element == null) {
element = PsiUtilCore.getElementAtOffset(file, offset);
}
return element;
}
示例3: reformatEnclosingExpressionListAtOffset
import com.intellij.psi.util.PsiUtilCore; //导入方法依赖的package包/类
private static void reformatEnclosingExpressionListAtOffset(@NotNull PsiFile file, int offset) {
final PsiElement elementAtOffset = PsiUtilCore.getElementAtOffset(file, offset);
PsiExpressionList listToReformat = getEnclosingExpressionList(elementAtOffset.getParent());
if (listToReformat != null) {
CodeStyleManager.getInstance(file.getProject()).reformat(listToReformat);
}
}
示例4: processText
import com.intellij.psi.util.PsiUtilCore; //导入方法依赖的package包/类
@Override
public void processText(Project project, Template template, Document document, RangeMarker templateRange, Editor editor) {
if (!template.getValue(Template.Property.USE_STATIC_IMPORT_IF_POSSIBLE)) {
return;
}
PsiDocumentManager.getInstance(project).commitDocument(document);
final PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project);
if (file == null) {
return;
}
List<Pair<PsiElement, StaticImporter>> staticImportTargets = new ArrayList<Pair<PsiElement, StaticImporter>>();
for (
PsiElement element = PsiUtilCore.getElementAtOffset(file, templateRange.getStartOffset());
element != null && element.getTextRange().getStartOffset() < templateRange.getEndOffset();
element = PsiTreeUtil.nextLeaf(element))
{
for (StaticImporter importer : IMPORTERS) {
if (importer.canPerform(element)) {
staticImportTargets.add(Pair.create(element, importer));
break;
}
}
}
Collections.reverse(staticImportTargets);
for (Pair<PsiElement, StaticImporter> pair : staticImportTargets) {
if (pair.first.isValid()) {
pair.second.perform(project, file, editor, pair.first);
}
}
}
示例5: preprocessOnPaste
import com.intellij.psi.util.PsiUtilCore; //导入方法依赖的package包/类
@Override
@NotNull
public String preprocessOnPaste(Project project, PsiFile file, Editor editor, String text, RawText rawText) {
final Document document = editor.getDocument();
PsiDocumentManager.getInstance(project).commitDocument(document);
int caretOffset = editor.getCaretModel().getOffset();
PsiElement element = PsiUtilCore.getElementAtOffset(file, caretOffset);
ASTNode node = element.getNode();
if (node != null) {
boolean hasMarkup = text.indexOf('>') >= 0 || text.indexOf('<') >= 0;
if (element.getTextOffset() == caretOffset &&
node.getElementType() == XmlTokenType.XML_END_TAG_START &&
node.getTreePrev().getElementType() == XmlTokenType.XML_TAG_END) {
return hasMarkup ? text : encode(text, element);
} else {
XmlElement parent = PsiTreeUtil.getParentOfType(element, XmlText.class, XmlAttributeValue.class);
if (parent != null) {
if (parent instanceof XmlText && hasMarkup) {
return text;
}
if (TreeUtil.findParent(node, XmlElementType.XML_CDATA) == null &&
TreeUtil.findParent(node, XmlElementType.XML_COMMENT) == null) {
return encode(text, element);
}
}
}
}
return text;
}
示例6: getDependency
import com.intellij.psi.util.PsiUtilCore; //导入方法依赖的package包/类
@Nullable
private static MavenDomDependency getDependency(PsiFile file, Editor editor) {
PsiElement el = PsiUtilCore.getElementAtOffset(file, editor.getCaretModel().getOffset());
XmlTag tag = PsiTreeUtil.getParentOfType(el, XmlTag.class, false);
if (tag == null) return null;
DomElement dom = DomManager.getDomManager(el.getProject()).getDomElement(tag);
if (dom == null) return null;
return dom.getParentOfType(MavenDomDependency.class, false);
}
示例7: calculateOffsetToInsertClosingBrace
import com.intellij.psi.util.PsiUtilCore; //导入方法依赖的package包/类
/**
* Current handler inserts closing curly brace (right brace) if necessary. There is a possible case that it should be located
* more than one line forward.
* <p/>
* <b>Example</b>
* <pre>
* if (test1()) {
* } else {<caret> if (test2()) {
* foo();
* }
* </pre>
* <p/>
* We want to get this after the processing:
* <pre>
* if (test1()) {
* } else {
* if (test2()) {
* foo();
* }
* }
* </pre>
* I.e. closing brace should be inserted two lines below current caret line. Hence, we need to calculate correct offset
* to use for brace inserting. This method is responsible for that.
* <p/>
* In essence it inspects PSI structure and finds PSE elements with the max length that starts at caret offset. End offset
* of that element is used as an insertion point.
*
* @param file target PSI file
* @param text text from the given file
* @param offset target offset where line feed will be inserted
* @return pair of (element, offset). The element is the '}' owner, if applicable; the offset is the position for inserting closing brace
*/
protected Pair<PsiElement, Integer> calculateOffsetToInsertClosingBrace(@NotNull PsiFile file, @NotNull CharSequence text, final int offset) {
PsiElement element = PsiUtilCore.getElementAtOffset(file, offset);
ASTNode node = element.getNode();
if (node != null && node.getElementType() == TokenType.WHITE_SPACE) {
return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
}
for (PsiElement parent = element.getParent(); parent != null; parent = parent.getParent()) {
ASTNode parentNode = parent.getNode();
if (parentNode == null || parentNode.getStartOffset() != offset) {
break;
}
element = parent;
}
if (element.getTextOffset() != offset) {
return Pair.create(null, CharArrayUtil.shiftForwardUntil(text, offset, "\n"));
}
return Pair.create(element, element.getTextRange().getEndOffset());
}
示例8: getSelectedElement
import com.intellij.psi.util.PsiUtilCore; //导入方法依赖的package包/类
@NotNull
public static PsiElement getSelectedElement(Editor editor, PsiFile file) {
return PsiUtilCore.getElementAtOffset(file, editor.getCaretModel().getOffset());
}