Java Code Examples for com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil#getEditorForInjectedLanguageNoCommit()

The following examples show how to use com.intellij.psi.impl.source.tree.injected.InjectedLanguageUtil#getEditorForInjectedLanguageNoCommit() . 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: LightPlatformCodeInsightTestCase.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public Object getData(@Nonnull Key<?> dataId) {
  if (PlatformDataKeys.EDITOR == dataId) {
    return myEditor;
  }
  if (dataId == AnActionEvent.injectedId(PlatformDataKeys.EDITOR)) {
    return InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(getEditor(), getFile());
  }
  if (LangDataKeys.PSI_FILE == dataId) {
    return myFile;
  }
  if (dataId == AnActionEvent.injectedId(LangDataKeys.PSI_FILE)) {
    Editor editor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(getEditor(), getFile());
    return editor instanceof EditorWindow ? ((EditorWindow)editor).getInjectedFile() : getFile();
  }
  return super.getData(dataId);
}
 
Example 2
Source File: CodeInsightTestFixtureImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void setupEditorForInjectedLanguage() {
  Editor editor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(myEditor, myFile);
  if (editor instanceof EditorWindow) {
    myFile = ((EditorWindow)editor).getInjectedFile();
    myEditor = editor;
  }
}
 
Example 3
Source File: LightPlatformCodeInsightTestCase.java    From consulo with Apache License 2.0 5 votes vote down vote up
private static void setupEditorForInjectedLanguage() {
  Editor editor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(myEditor, myFile);
  if (editor instanceof EditorWindow) {
    myFile = ((EditorWindow)editor).getInjectedFile();
    myEditor = editor;
  }
}
 
Example 4
Source File: CtrlMouseHandler.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private EditorEx getPossiblyInjectedEditor() {
  final Document document = myHostEditor.getDocument();
  if (PsiDocumentManager.getInstance(myProject).isCommitted(document)) {
    PsiFile psiFile = PsiDocumentManager.getInstance(myProject).getPsiFile(document);
    return (EditorEx)InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(myHostEditor, psiFile, myHostOffset);
  }
  return myHostEditor;
}
 
Example 5
Source File: BaseCodeInsightAction.java    From consulo with Apache License 2.0 5 votes vote down vote up
@RequiredUIAccess
public static Editor getInjectedEditor(@Nonnull Project project, final Editor editor, boolean commit) {
  Editor injectedEditor = editor;
  if (editor != null) {
    PsiDocumentManager documentManager = PsiDocumentManager.getInstance(project);
    PsiFile psiFile = documentManager.getCachedPsiFile(editor.getDocument());
    if (psiFile != null) {
      if (commit) documentManager.commitAllDocuments();
      injectedEditor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(editor, psiFile);
    }
  }
  return injectedEditor;
}
 
Example 6
Source File: CustomTemplateCallback.java    From consulo with Apache License 2.0 5 votes vote down vote up
public CustomTemplateCallback(@Nonnull Editor editor, @Nonnull PsiFile file) {
  myProject = file.getProject();
  myTemplateManager = TemplateManager.getInstance(myProject);

  int parentEditorOffset = getOffset(editor);
  PsiElement element = InjectedLanguageManager.getInstance(file.getProject()).findInjectedElementAt(file, parentEditorOffset);
  myFile = element != null ? element.getContainingFile() : file;

  myInInjectedFragment = InjectedLanguageManager.getInstance(myProject).isInjectedFragment(myFile);
  myEditor = myInInjectedFragment ? InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(editor, file, parentEditorOffset) : editor;
  myOffset = myInInjectedFragment ? getOffset(myEditor) : parentEditorOffset;
}
 
Example 7
Source File: PsiAwareFileEditorManagerImpl.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Override
protected Editor getOpenedEditor(@Nonnull final Editor editor, final boolean focusEditor) {
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(getProject());
  Document document = editor.getDocument();
  PsiFile psiFile = documentManager.getPsiFile(document);
  if (!focusEditor || documentManager.isUncommited(document)) {
    return editor;
  }

  return InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(editor, psiFile);
}
 
Example 8
Source File: CodeInsightTestFixtureImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Nullable
protected Editor getCompletionEditor() {
  return InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(myEditor, myFile);
}
 
Example 9
Source File: IdentifierHighlighterPass.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void doCollectInformation(@Nonnull final ProgressIndicator progress) {
  if (myHighlightUsagesHandler != null) {
    List<PsiElement> targets = myHighlightUsagesHandler.getTargets();
    myHighlightUsagesHandler.computeUsages(targets);
    final List<TextRange> readUsages = myHighlightUsagesHandler.getReadUsages();
    for (TextRange readUsage : readUsages) {
      LOG.assertTrue(readUsage != null, "null text range from " + myHighlightUsagesHandler);
    }
    myReadAccessRanges.addAll(readUsages);
    final List<TextRange> writeUsages = myHighlightUsagesHandler.getWriteUsages();
    for (TextRange writeUsage : writeUsages) {
      LOG.assertTrue(writeUsage != null, "null text range from " + myHighlightUsagesHandler);
    }
    myWriteAccessRanges.addAll(writeUsages);
    if (!myHighlightUsagesHandler.highlightReferences()) return;
  }

  Set<String> flags = ContainerUtil.newHashSet(TargetElementUtilEx.ELEMENT_NAME_ACCEPTED, TargetElementUtilEx.REFERENCED_ELEMENT_ACCEPTED);
  PsiElement myTarget = TargetElementUtil.findTargetElement(myEditor, flags, myCaretOffset);

  if (myTarget == null) {
    if (!PsiDocumentManager.getInstance(myProject).isUncommited(myEditor.getDocument())) {
      // when document is committed, try to check injected stuff - it's fast
      Editor injectedEditor = InjectedLanguageUtil.getEditorForInjectedLanguageNoCommit(myEditor, myFile, myCaretOffset);
      myTarget = TargetElementUtil.findTargetElement(injectedEditor, flags, injectedEditor.getCaretModel().getOffset());
    }
  }

  if (myTarget != null) {
    highlightTargetUsages(myTarget);
  } else {
    PsiReference ref = TargetElementUtil.findReference(myEditor);
    if (ref instanceof PsiPolyVariantReference) {
      if (!ref.getElement().isValid()) {
        throw new PsiInvalidElementAccessException(ref.getElement(), "Invalid element in " + ref + " of " + ref.getClass() + "; editor=" + myEditor);
      }
      ResolveResult[] results = ((PsiPolyVariantReference)ref).multiResolve(false);
      if (results.length > 0) {
        for (ResolveResult result : results) {
          PsiElement target = result.getElement();
          if (target != null) {
            if (!target.isValid()) {
              throw new PsiInvalidElementAccessException(target, "Invalid element returned from " + ref + " of " + ref.getClass() + "; editor=" + myEditor);
            }
            highlightTargetUsages(target);
          }
        }
      }
    }

  }
}