Java Code Examples for com.intellij.injected.editor.DocumentWindow#injectedToHost()

The following examples show how to use com.intellij.injected.editor.DocumentWindow#injectedToHost() . 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: TabOutScopesTrackerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void registerEmptyScope(@Nonnull Editor editor, int offset, int tabOutOffset) {
  ApplicationManager.getApplication().assertIsDispatchThread();
  if (editor.isDisposed()) throw new IllegalArgumentException("Editor is already disposed");
  if (tabOutOffset <= offset) throw new IllegalArgumentException("tabOutOffset should be larger than offset");

  if (!CodeInsightSettings.getInstance().TAB_EXITS_BRACKETS_AND_QUOTES) return;

  if (editor instanceof EditorWindow) {
    DocumentWindow documentWindow = ((EditorWindow)editor).getDocument();
    offset = documentWindow.injectedToHost(offset);
    editor = ((EditorWindow)editor).getDelegate();
  }
  if (!(editor instanceof DesktopEditorImpl)) return;

  Tracker tracker = Tracker.forEditor((DesktopEditorImpl)editor, true);
  tracker.registerScope(offset, tabOutOffset - offset);
}
 
Example 2
Source File: TabOutScopesTrackerImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static int checkOrRemoveScopeEndingAt(@Nonnull Editor editor, int offset, boolean removeScope) {
  ApplicationManager.getApplication().assertIsDispatchThread();

  if (!CodeInsightSettings.getInstance().TAB_EXITS_BRACKETS_AND_QUOTES) return 0;

  if (editor instanceof EditorWindow) {
    DocumentWindow documentWindow = ((EditorWindow)editor).getDocument();
    offset = documentWindow.injectedToHost(offset);
    editor = ((EditorWindow)editor).getDelegate();
  }
  if (!(editor instanceof DesktopEditorImpl)) return 0;

  Tracker tracker = Tracker.forEditor((DesktopEditorImpl)editor, false);
  if (tracker == null) return 0;

  return tracker.getCaretShiftForScopeEndingAt(offset, removeScope);
}
 
Example 3
Source File: LookupImpl.java    From consulo with Apache License 2.0 6 votes vote down vote up
private static int insertLookupInDocumentWindowIfNeeded(Project project, Editor editor, int caretOffset, int prefix, String lookupString) {
  DocumentWindow document = getInjectedDocument(project, editor, caretOffset);
  if (document == null) return insertLookupInDocument(caretOffset, editor.getDocument(), prefix, lookupString);
  PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(document);
  int offset = document.hostToInjected(caretOffset);
  int lookupStart = Math.min(offset, Math.max(offset - prefix, 0));
  int diff = -1;
  if (file != null) {
    List<TextRange> ranges = InjectedLanguageManager.getInstance(project).intersectWithAllEditableFragments(file, TextRange.create(lookupStart, offset));
    if (!ranges.isEmpty()) {
      diff = ranges.get(0).getStartOffset() - lookupStart;
      if (ranges.size() == 1 && diff == 0) diff = -1;
    }
  }
  if (diff == -1) return insertLookupInDocument(caretOffset, editor.getDocument(), prefix, lookupString);
  return document.injectedToHost(insertLookupInDocument(offset, document, prefix - diff, diff == 0 ? lookupString : lookupString.substring(diff)));
}
 
Example 4
Source File: GraphQLBlockWrapper.java    From js-graphql-intellij-plugin with MIT License 5 votes vote down vote up
@NotNull
private List<Block> buildInjectedBlocks() {

    PsiElement psi = myNode.getPsi();
    if (psi == null) {
        return EMPTY;
    }
    PsiFile file = psi.getContainingFile();
    if (file == null) {
        return EMPTY;
    }

    if (InjectedLanguageUtil.getCachedInjectedDocuments(file).isEmpty()) {
        return EMPTY;
    }

    TextRange blockRange = myNode.getTextRange();
    List<DocumentWindow> documentWindows = InjectedLanguageUtil.getCachedInjectedDocuments(file);
    for (DocumentWindow documentWindow : documentWindows) {
        int startOffset = documentWindow.injectedToHost(0);
        int endOffset = startOffset + documentWindow.getTextLength();
        if (blockRange.containsRange(startOffset, endOffset)) {
            PsiFile injected = PsiDocumentManager.getInstance(psi.getProject()).getCachedPsiFile(documentWindow);
            if (injected != null) {
                List<Block> result = ContainerUtilRt.newArrayList();
                GraphQLInjectedLanguageBlockBuilder builder = new GraphQLInjectedLanguageBlockBuilder(settings);
                builder.addInjectedBlocks(result, myNode, getWrap(), getAlignment(), getIndent());
                return result;
            }
        }
    }
    return EMPTY;
}
 
Example 5
Source File: LocalInspectionsPass.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void addDescriptorsFromInjectedResults(@Nonnull InspectionManager iManager, @Nonnull GlobalInspectionContextImpl context) {
  InjectedLanguageManager ilManager = InjectedLanguageManager.getInstance(myProject);
  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myProject);

  for (Map.Entry<PsiFile, List<InspectionResult>> entry : result.entrySet()) {
    PsiFile file = entry.getKey();
    if (file == getFile()) continue; // not injected
    DocumentWindow documentRange = (DocumentWindow)documentManager.getDocument(file);
    List<InspectionResult> resultList = entry.getValue();
    for (InspectionResult inspectionResult : resultList) {
      LocalInspectionToolWrapper toolWrapper = inspectionResult.tool;
      for (ProblemDescriptor descriptor : inspectionResult.foundProblems) {

        PsiElement psiElement = descriptor.getPsiElement();
        if (psiElement == null) continue;
        if (SuppressionUtil.inspectionResultSuppressed(psiElement, toolWrapper.getTool())) continue;
        List<TextRange> editables = ilManager.intersectWithAllEditableFragments(file, ((ProblemDescriptorBase)descriptor).getTextRange());
        for (TextRange editable : editables) {
          TextRange hostRange = documentRange.injectedToHost(editable);
          QuickFix[] fixes = descriptor.getFixes();
          LocalQuickFix[] localFixes = null;
          if (fixes != null) {
            localFixes = new LocalQuickFix[fixes.length];
            for (int k = 0; k < fixes.length; k++) {
              QuickFix fix = fixes[k];
              localFixes[k] = (LocalQuickFix)fix;
            }
          }
          ProblemDescriptor patchedDescriptor =
                  iManager.createProblemDescriptor(getFile(), hostRange, descriptor.getDescriptionTemplate(), descriptor.getHighlightType(), true,
                                                   localFixes);
          addDescriptors(toolWrapper, patchedDescriptor, context);
        }
      }
    }
  }
}
 
Example 6
Source File: AbstractBlock.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
private List<Block> buildInjectedBlocks() {
  if (myBuildIndentsOnly) {
    return EMPTY;
  }
  if (!(this instanceof SettingsAwareBlock)) {
    return EMPTY;
  }
  PsiElement psi = myNode.getPsi();
  if (psi == null) {
    return EMPTY;
  }
  PsiFile file = psi.getContainingFile();
  if (file == null) {
    return EMPTY;
  }

  if (InjectedLanguageUtil.getCachedInjectedDocuments(file).isEmpty()) {
    return EMPTY;
  }

  TextRange blockRange = myNode.getTextRange();
  List<DocumentWindow> documentWindows = InjectedLanguageUtil.getCachedInjectedDocuments(file);
  for (DocumentWindow documentWindow : documentWindows) {
    int startOffset = documentWindow.injectedToHost(0);
    int endOffset = startOffset + documentWindow.getTextLength();
    if (blockRange.containsRange(startOffset, endOffset)) {
      PsiFile injected = PsiDocumentManager.getInstance(psi.getProject()).getCachedPsiFile(documentWindow);
      if (injected != null) {
        List<Block> result = ContainerUtilRt.newArrayList();
        DefaultInjectedLanguageBlockBuilder builder = new DefaultInjectedLanguageBlockBuilder(((SettingsAwareBlock)this).getSettings());
        builder.addInjectedBlocks(result, myNode, getWrap(), getAlignment(), getIndent());
        return result;
      }
    }
  }
  return EMPTY;
}
 
Example 7
Source File: InjectedLanguageManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
@Nonnull
public TextRange injectedToHost(@Nonnull PsiElement injectedContext, @Nonnull TextRange injectedTextRange) {
  DocumentWindow documentWindow = getDocumentWindow(injectedContext);
  return documentWindow == null ? injectedTextRange : documentWindow.injectedToHost(injectedTextRange);
}
 
Example 8
Source File: InjectedLanguageManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public int injectedToHost(@Nonnull PsiElement element, int offset) {
  DocumentWindow documentWindow = getDocumentWindow(element);
  return documentWindow == null ? offset : documentWindow.injectedToHost(offset);
}
 
Example 9
Source File: InjectedLanguageManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public int injectedToHost(@Nonnull PsiElement injectedContext, int injectedOffset, boolean minHostOffset) {
  DocumentWindow documentWindow = getDocumentWindow(injectedContext);
  return documentWindow == null ? injectedOffset : documentWindow.injectedToHost(injectedOffset, minHostOffset);
}
 
Example 10
Source File: CodeStyleManagerRunnable.java    From consulo with Apache License 2.0 4 votes vote down vote up
public T perform(PsiFile file, int offset, @Nullable TextRange range, T defaultValue) {
  if (file instanceof PsiCompiledFile) {
    file = ((PsiCompiledFile)file).getDecompiledPsiFile();
  }

  PsiDocumentManager documentManager = PsiDocumentManager.getInstance(myCodeStyleManager.getProject());
  Document document = documentManager.getDocument(file);
  if (document instanceof DocumentWindow) {
    final DocumentWindow documentWindow = (DocumentWindow)document;
    final PsiFile topLevelFile = InjectedLanguageManager.getInstance(file.getProject()).getTopLevelFile(file);
    if (!file.equals(topLevelFile)) {
      if (range != null) {
        range = documentWindow.injectedToHost(range);
      }
      if (offset != -1) {
        offset = documentWindow.injectedToHost(offset);
      }
      return adjustResultForInjected(perform(topLevelFile, offset, range, defaultValue), documentWindow);
    }
  }

  final PsiFile templateFile = PsiUtilCore.getTemplateLanguageFile(file);
  if (templateFile != null) {
    file = templateFile;
    document = documentManager.getDocument(templateFile);
  }

  PsiElement element = null;
  if (offset != -1) {
    element = CodeStyleManagerImpl.findElementInTreeWithFormatterEnabled(file, offset);
    if (element == null && offset != file.getTextLength()) {
      return defaultValue;
    }
    if (isInsidePlainComment(offset, element)) {
      return computeValueInsidePlainComment(file, offset, defaultValue);
    }
  }

  final FormattingModelBuilder builder = LanguageFormatting.INSTANCE.forContext(file);
  FormattingModelBuilder elementBuilder = element != null ? LanguageFormatting.INSTANCE.forContext(element) : builder;
  if (builder != null && elementBuilder != null) {
    mySettings = CodeStyle.getSettings(file);

    mySignificantRange = offset != -1 ? getSignificantRange(file, offset) : null;
    myIndentOptions = mySettings.getIndentOptionsByFile(file, mySignificantRange);

    FormattingMode currentMode = myCodeStyleManager.getCurrentFormattingMode();
    myCodeStyleManager.setCurrentFormattingMode(myMode);
    try {
      myModel = buildModel(builder, file, document);
      T result = doPerform(offset, range);
      if (result != null) {
        return result;
      }
    }
    finally {
      myCodeStyleManager.setCurrentFormattingMode(currentMode);
    }
  }
  return defaultValue;
}