本文整理汇总了Java中com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil类的典型用法代码示例。如果您正苦于以下问题:Java InjectedLanguageUtil类的具体用法?Java InjectedLanguageUtil怎么用?Java InjectedLanguageUtil使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
InjectedLanguageUtil类属于com.intellij.psi.impl.source.tree.injected包,在下文中一共展示了InjectedLanguageUtil类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: tokenize
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
@Override
public void tokenize(@NotNull PsiLiteralExpression element, TokenConsumer consumer) {
PsiLiteralExpressionImpl literalExpression = (PsiLiteralExpressionImpl)element;
if (literalExpression.getLiteralElementType() != JavaTokenType.STRING_LITERAL) return; // not a string literal
String text = literalExpression.getInnerText();
if (StringUtil.isEmpty(text) || text.length() <= 2) { // optimisation to avoid expensive injection check
return;
}
if (InjectedLanguageUtil.hasInjections(literalExpression)) return;
final PsiModifierListOwner listOwner = PsiTreeUtil.getParentOfType(element, PsiModifierListOwner.class);
if (listOwner != null && AnnotationUtil.isAnnotated(listOwner, Collections.singleton(AnnotationUtil.NON_NLS), false, false)) {
return;
}
if (!text.contains("\\")) {
consumer.consumeToken(element, PlainTextSplitter.getInstance());
}
else {
processTextWithEscapeSequences(element, text, consumer);
}
}
示例2: setupEditorForInjectedLanguage
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
private static void setupEditorForInjectedLanguage() {
if (myEditor != null) {
final Ref<EditorWindow> editorWindowRef = new Ref<EditorWindow>();
myEditor.getCaretModel().runForEachCaret(new CaretAction() {
@Override
public void perform(Caret caret) {
Editor editor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(myEditor, myFile);
if (caret == myEditor.getCaretModel().getPrimaryCaret() && editor instanceof EditorWindow) {
editorWindowRef.set((EditorWindow)editor);
}
}
});
if (!editorWindowRef.isNull()) {
myEditor = editorWindowRef.get();
myFile = editorWindowRef.get().getInjectedFile();
}
}
}
示例3: match
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
void match(PsiElement element, final Language language) {
final MatchingStrategy strategy = matchContext.getPattern().getStrategy();
final Language elementLanguage = element.getLanguage();
if (strategy.continueMatching(element) && elementLanguage.isKindOf(language)) {
visitor.matchContext(new ArrayBackedNodeIterator(new PsiElement[] {element}));
return;
}
for(PsiElement el=element.getFirstChild();el!=null;el=el.getNextSibling()) {
match(el, language);
}
if (element instanceof PsiLanguageInjectionHost) {
InjectedLanguageUtil.enumerate(element, new PsiLanguageInjectionHost.InjectedPsiVisitor() {
@Override
public void visit(@NotNull PsiFile injectedPsi, @NotNull List<PsiLanguageInjectionHost.Shred> places) {
match(injectedPsi, language);
}
});
}
}
示例4: shouldSkipAutoPopup
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
private static boolean shouldSkipAutoPopup(Editor editor, PsiFile psiFile) {
int offset = editor.getCaretModel().getOffset();
int psiOffset = Math.max(0, offset - 1);
PsiElement elementAt = InjectedLanguageUtil.findInjectedElementNoCommit(psiFile, psiOffset);
if (elementAt == null) {
elementAt = psiFile.findElementAt(psiOffset);
}
if (elementAt == null) return true;
Language language = PsiUtilCore.findLanguageFromElement(elementAt);
for (CompletionConfidence confidence : CompletionConfidenceEP.forLanguage(language)) {
final ThreeState result = confidence.shouldSkipAutopopup(elementAt, psiFile, offset);
if (result != ThreeState.UNSURE) {
LOG.debug(confidence + " has returned shouldSkipAutopopup=" + result);
return result == ThreeState.YES;
}
}
return false;
}
示例5: createCompletionContext
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
private static CompletionContext createCompletionContext(PsiFile hostCopy,
int hostStartOffset,
OffsetMap hostMap, PsiFile originalFile) {
CompletionAssertions.assertHostInfo(hostCopy, hostMap);
InjectedLanguageManager injectedLanguageManager = InjectedLanguageManager.getInstance(hostCopy.getProject());
CompletionContext context;
PsiFile injected = InjectedLanguageUtil.findInjectedPsiNoCommit(hostCopy, hostStartOffset);
if (injected != null) {
if (injected instanceof PsiFileImpl) {
((PsiFileImpl)injected).setOriginalFile(originalFile);
}
DocumentWindow documentWindow = InjectedLanguageUtil.getDocumentWindow(injected);
CompletionAssertions.assertInjectedOffsets(hostStartOffset, injectedLanguageManager, injected, documentWindow);
context = new CompletionContext(injected, translateOffsetMapToInjected(hostMap, documentWindow));
} else {
context = new CompletionContext(hostCopy, hostMap);
}
CompletionAssertions.assertFinalOffsets(originalFile, context, injected);
return context;
}
示例6: rememberDocumentState
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
private static Runnable rememberDocumentState(final Editor _editor) {
final Editor editor = InjectedLanguageUtil.getTopLevelEditor(_editor);
final String documentText = editor.getDocument().getText();
final int caret = editor.getCaretModel().getOffset();
final int selStart = editor.getSelectionModel().getSelectionStart();
final int selEnd = editor.getSelectionModel().getSelectionEnd();
final int vOffset = editor.getScrollingModel().getVerticalScrollOffset();
final int hOffset = editor.getScrollingModel().getHorizontalScrollOffset();
return new Runnable() {
@Override
public void run() {
DocumentEx document = (DocumentEx) editor.getDocument();
document.replaceString(0, document.getTextLength(), documentText);
editor.getCaretModel().moveToOffset(caret);
editor.getSelectionModel().setSelection(selStart, selEnd);
editor.getScrollingModel().scrollHorizontally(hOffset);
editor.getScrollingModel().scrollVertically(vOffset);
}
};
}
示例7: iterateOverCarets
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
private static void iterateOverCarets(@NotNull final Project project,
@NotNull final Editor hostEditor,
@NotNull final MultiCaretCodeInsightActionHandler handler) {
PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
final PsiFile psiFile = documentManager.getCachedPsiFile(hostEditor.getDocument());
documentManager.commitAllDocuments();
hostEditor.getCaretModel().runForEachCaret(new CaretAction() {
@Override
public void perform(Caret caret) {
Editor editor = hostEditor;
if (psiFile != null) {
Caret injectedCaret = InjectedLanguageUtil.getCaretForInjectedLanguageNoCommit(caret, psiFile);
if (injectedCaret != null) {
caret = injectedCaret;
editor = caret.getEditor();
}
}
final PsiFile file = PsiUtilBase.getPsiFileInEditor(caret, project);
if (file != null) {
handler.invoke(project, editor, caret, file);
}
}
});
}
示例8: injectedEditorIfCharTypedIsSignificant
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
@NotNull
public static Editor injectedEditorIfCharTypedIsSignificant(final char charTyped, @NotNull Editor editor, @NotNull PsiFile oldFile) {
int offset = editor.getCaretModel().getOffset();
// even for uncommitted document try to retrieve injected fragment that has been there recently
// we are assuming here that when user is (even furiously) typing, injected language would not change
// and thus we can use its lexer to insert closing braces etc
for (DocumentWindow documentWindow : InjectedLanguageUtil.getCachedInjectedDocuments(oldFile)) {
if (documentWindow.isValid() && documentWindow.containsRange(offset, offset)) {
PsiFile injectedFile = PsiDocumentManager.getInstance(oldFile.getProject()).getPsiFile(documentWindow);
final Editor injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(editor, injectedFile);
// IDEA-52375 fix: last quote sign should be handled by outer language quote handler
final CharSequence charsSequence = editor.getDocument().getCharsSequence();
if (injectedEditor.getCaretModel().getOffset() == injectedEditor.getDocument().getTextLength() &&
offset < charsSequence.length() && charTyped == charsSequence.charAt(offset)) {
return editor;
}
return injectedEditor;
}
}
return editor;
}
示例9: getContext
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的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;
}
示例10: insertLookupString
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
private void insertLookupString(LookupElement item, final int prefix) {
final String lookupString = getCaseCorrectedLookupString(item);
final Editor hostEditor = InjectedLanguageUtil.getTopLevelEditor(myEditor);
hostEditor.getCaretModel().runForEachCaret(new CaretAction() {
@Override
public void perform(Caret caret) {
EditorModificationUtil.deleteSelectedText(hostEditor);
final int caretOffset = hostEditor.getCaretModel().getOffset();
int lookupStart = Math.min(caretOffset, Math.max(caretOffset - prefix, 0));
int len = hostEditor.getDocument().getTextLength();
LOG.assertTrue(lookupStart >= 0 && lookupStart <= len,
"ls: " + lookupStart + " caret: " + caretOffset + " prefix:" + prefix + " doc: " + len);
LOG.assertTrue(caretOffset >= 0 && caretOffset <= len, "co: " + caretOffset + " doc: " + len);
hostEditor.getDocument().replaceString(lookupStart, caretOffset, lookupString);
int offset = lookupStart + lookupString.length();
hostEditor.getCaretModel().moveToOffset(offset);
hostEditor.getSelectionModel().removeSelection();
}
});
myEditor.getScrollingModel().scrollToCaret(ScrollType.RELATIVE);
}
示例11: maySaveDocument
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
@Override
public boolean maySaveDocument(@NotNull Document document, boolean isSaveExplicit) {
if (ApplicationManager.getApplication().isDisposed() || isSaveExplicit) {
return true;
}
for (Project project : ProjectManager.getInstance().getOpenProjects()) {
if (!project.isInitialized() || project.isDisposed()) {
continue;
}
LookupEx lookup = LookupManager.getInstance(project).getActiveLookup();
if (lookup != null) {
Editor editor = InjectedLanguageUtil.getTopLevelEditor(lookup.getEditor());
if (editor.getDocument() == document) {
return false;
}
}
}
return true;
}
示例12: chooseBetweenHostAndInjected
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
@Nullable
public static Pair<PsiFile,Editor> chooseBetweenHostAndInjected(@NotNull PsiFile hostFile, @NotNull Editor hostEditor, @NotNull PairProcessor<PsiFile, Editor> predicate) {
Editor editorToApply = null;
PsiFile fileToApply = null;
int offset = hostEditor.getCaretModel().getOffset();
PsiFile injectedFile = InjectedLanguageUtil.findInjectedPsiNoCommit(hostFile, offset);
if (injectedFile != null) {
Editor injectedEditor = InjectedLanguageUtil.getInjectedEditorForInjectedFile(hostEditor, injectedFile);
if (predicate.process(injectedFile, injectedEditor)) {
editorToApply = injectedEditor;
fileToApply = injectedFile;
}
}
if (editorToApply == null && predicate.process(hostFile, hostEditor)) {
editorToApply = hostEditor;
fileToApply = hostFile;
}
if (editorToApply == null) return null;
return Pair.create(fileToApply, editorToApply);
}
示例13: caretPositionChanged
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
@Override
public void caretPositionChanged(CaretEvent e) {
if (!available() || myEditor.getSelectionModel().hasSelection()) return;
final ViewerTreeStructure treeStructure = (ViewerTreeStructure)myPsiTreeBuilder.getTreeStructure();
final PsiElement rootPsiElement = treeStructure.getRootPsiElement();
if (rootPsiElement == null) return;
final PsiElement rootElement = ((ViewerTreeStructure)myPsiTreeBuilder.getTreeStructure()).getRootPsiElement();
int baseOffset = rootPsiElement.getTextRange().getStartOffset();
final int offset = myEditor.getCaretModel().getOffset() + baseOffset;
final PsiElement element = InjectedLanguageUtil.findElementAtNoCommit(rootElement.getContainingFile(), offset);
if (element != null && myBlockTreeBuilder != null) {
TextRange rangeInHostFile = InjectedLanguageManager.getInstance(myProject).injectedToHost(element, element.getTextRange());
selectBlockNode(findBlockNode(rangeInHostFile, true));
}
myPsiTreeBuilder.select(element);
}
示例14: selectionChanged
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
@Override
public void selectionChanged(SelectionEvent e) {
if (!available() || !myEditor.getSelectionModel().hasSelection()) return;
ViewerTreeStructure treeStructure = (ViewerTreeStructure)myPsiTreeBuilder.getTreeStructure();
if (treeStructure == null) return;
final PsiElement rootElement = treeStructure.getRootPsiElement();
if (rootElement == null) return;
final SelectionModel selection = myEditor.getSelectionModel();
final TextRange textRange = rootElement.getTextRange();
int baseOffset = textRange != null ? textRange.getStartOffset() : 0;
final int start = selection.getSelectionStart()+baseOffset;
final int end = selection.getSelectionEnd()+baseOffset - 1;
final PsiElement element =
findCommonParent(InjectedLanguageUtil.findElementAtNoCommit(rootElement.getContainingFile(), start),
InjectedLanguageUtil.findElementAtNoCommit(rootElement.getContainingFile(), end));
if (element != null && myBlockTreeBuilder != null) {
if (myEditor.getContentComponent().hasFocus()) {
TextRange rangeInHostFile = InjectedLanguageManager.getInstance(myProject).injectedToHost(element, element.getTextRange());
selectBlockNode(findBlockNode(rangeInHostFile, true));
}
}
myPsiTreeBuilder.select(element);
}
示例15: containsWhiteSpaceSymbolsOnly
import com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil; //导入依赖的package包/类
@Override
public boolean containsWhiteSpaceSymbolsOnly(int startOffset, int endOffset) {
WhiteSpaceFormattingStrategy strategy = myWhiteSpaceStrategy;
if (strategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset) {
return true;
}
PsiElement injectedElement = myFile != null ? InjectedLanguageUtil.findElementAtNoCommit(myFile, startOffset) : null;
if (injectedElement != null) {
Language injectedLanguage = injectedElement.getLanguage();
if (!injectedLanguage.equals(myFile.getLanguage())) {
WhiteSpaceFormattingStrategy localStrategy = WhiteSpaceFormattingStrategyFactory.getStrategy(injectedLanguage);
if (localStrategy != null) {
return localStrategy.check(myDocument.getCharsSequence(), startOffset, endOffset) >= endOffset;
}
}
}
return false;
}