Java Code Examples for com.intellij.psi.PsiDocumentManager#doPostponedOperationsAndUnblockDocument()

The following examples show how to use com.intellij.psi.PsiDocumentManager#doPostponedOperationsAndUnblockDocument() . 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: YamlCreateServiceArgumentsCallback.java    From idea-php-symfony2-plugin with MIT License 6 votes vote down vote up
@Override
public void insert(List<String> items) {
    PsiDocumentManager manager = PsiDocumentManager.getInstance(serviceKeyValue.getProject());
    Document document = manager.getDocument(serviceKeyValue.getContainingFile());
    if (document == null) {
        return;
    }

    List<String> arrayList = new ArrayList<>();
    for (String item : items) {
        arrayList.add("'@" + (StringUtils.isNotBlank(item) ? item : "?") + "'");
    }

    YamlHelper.putKeyValue(serviceKeyValue, "arguments", "[" + StringUtils.join(arrayList, ", ") + "]");

    manager.doPostponedOperationsAndUnblockDocument(document);
    manager.commitDocument(document);
}
 
Example 2
Source File: CodeInsightUtilCore.java    From consulo with Apache License 2.0 6 votes vote down vote up
@RequiredReadAction
public static <T extends PsiElement> T forcePsiPostprocessAndRestoreElement(@Nonnull T element,
                                                                            boolean useFileLanguage) {
  final PsiFile psiFile = element.getContainingFile();
  final Document document = psiFile.getViewProvider().getDocument();
  //if (document == null) return element;
  final Language language = useFileLanguage ? psiFile.getLanguage() : PsiUtilCore.getDialect(element);
  final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(psiFile.getProject());
  final RangeMarker rangeMarker = document.createRangeMarker(element.getTextRange());
  documentManager.doPostponedOperationsAndUnblockDocument(document);
  documentManager.commitDocument(document);

  T elementInRange = findElementInRange(psiFile, rangeMarker.getStartOffset(), rangeMarker.getEndOffset(),
                                        (Class<? extends T>)element.getClass(),
                                        language, element);
  rangeMarker.dispose();
  return elementInRange;
}
 
Example 3
Source File: DependenciesOptimizer.java    From Buck-IntelliJ-Plugin with Apache License 2.0 5 votes vote down vote up
public static void optimzeDeps(@NotNull PsiFile file) {
  final PropertyVisitor visitor = new PropertyVisitor();
  file.accept(new BuckVisitor() {
    @Override
    public void visitElement(PsiElement node) {
      node.acceptChildren(this);
      node.accept(visitor);
    }
  });

  // Commit modifications
  final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
  manager.doPostponedOperationsAndUnblockDocument(manager.getDocument(file));
}
 
Example 4
Source File: DependenciesOptimizer.java    From buck with Apache License 2.0 5 votes vote down vote up
private void optimizeDeps(@NotNull PsiFile file) {
  final PropertyVisitor visitor = new PropertyVisitor();
  file.accept(
      new BuckVisitor() {
        @Override
        public void visitElement(PsiElement node) {
          node.acceptChildren(this);
          node.accept(visitor);
        }
      });

  // Commit modifications.
  final PsiDocumentManager manager = PsiDocumentManager.getInstance(file.getProject());
  manager.doPostponedOperationsAndUnblockDocument(manager.getDocument(file));
}
 
Example 5
Source File: HaxeImportOptimizer.java    From intellij-haxe with Apache License 2.0 4 votes vote down vote up
private static void reorderImports(final PsiFile file) {
  HaxeFileModel fileModel = HaxeFileModel.fromElement(file);
  List<HaxeImportStatement> allImports = fileModel.getImportStatements();

  if (allImports.size() < 2) {
    return;
  }

  final HaxeImportStatement firstImport = allImports.get(0);
  int startOffset = firstImport.getStartOffsetInParent();
  final HaxeImportStatement lastImport = allImports.get(allImports.size() - 1);
  int endOffset = lastImport.getStartOffsetInParent() + lastImport.getText().length();

  // We assume the common practice of placing all imports in a single "block" at the top of a file. If there is something else (comments,
  // code, etc) there we just stop reordering to prevent data loss.
  for (PsiElement child : file.getChildren()) {
    int childOffset = child.getStartOffsetInParent();
    if (childOffset >= startOffset && childOffset <= endOffset
        && !(child instanceof HaxeImportStatement)
        && !(child instanceof PsiWhiteSpace)) {
      return;
    }
  }

  List<String> sortedImports = new ArrayList<>();

  for (HaxeImportStatement currentImport : allImports) {
    sortedImports.add(currentImport.getText());
  }

  sortedImports.sort(String::compareToIgnoreCase);

  final PsiDocumentManager psiDocumentManager = PsiDocumentManager.getInstance(file.getProject());
  final Document document = psiDocumentManager.getDocument(file);
  if (document != null) {
    final PsiDocumentManager documentManager = PsiDocumentManager.getInstance(file.getProject());

    /* This operation trims the document if necessary (e.g. it happens with "\n" at the very beginning).
       Need to reevaluate offsets here.
     */
    documentManager.doPostponedOperationsAndUnblockDocument(document);

    // Reevaluating offset values according to the previous comment.
    startOffset = firstImport.getStartOffsetInParent();
    endOffset = lastImport.getStartOffsetInParent() + lastImport.getText().length();

    document.deleteString(startOffset, endOffset);

    StringBuilder sortedImportsText = new StringBuilder();
    for (String sortedImport : sortedImports) {
      sortedImportsText.append(sortedImport);
      sortedImportsText.append("\n");
    }
    // Removes last "\n".
    CharSequence sortedImportsTextTrimmed = sortedImportsText.subSequence(0, sortedImportsText.length() - 1);

    documentManager.doPostponedOperationsAndUnblockDocument(document);
    document.insertString(startOffset, sortedImportsTextTrimmed);
  }

  // TODO Reorder usings.
}