private void checkPsiIsCorrect(final FileViewProvider key) {
    PsiFile actualPsi = key.getPsi(key.getBaseLanguage());

    PsiTreeDebugBuilder treeDebugBuilder =
        new PsiTreeDebugBuilder().setShowErrorElements(false).setShowWhiteSpaces(false);

    String actualPsiTree = treeDebugBuilder.psiToString(actualPsi);

    String fileName = key.getVirtualFile().getName();
    PsiFile psi =
        PsiFileFactory.getInstance(myProject)
            .createFileFromText(
                fileName,
                FileTypeManager.getInstance().getFileTypeByFileName(fileName),
                actualPsi.getNode().getText(),
                LocalTimeCounter.currentTime(),
                false);

    if (actualPsi.getClass().equals(psi.getClass())) {
      String expectedPsi = treeDebugBuilder.psiToString(psi);

      if (!expectedPsi.equals(actualPsiTree)) {
        myReformatElements.clear();
        assert expectedPsi.equals(actualPsiTree)
            : "Refactored psi should be the same as result of parsing";
      }
    }
  }
예제 #2
0
 public void remove(DocumentImpl subj, int startIndex, int endIndex, CharSequence toDelete) {
   DocumentEvent event = beforeChangedUpdate(subj, startIndex, toDelete, null, false);
   startIndex += myStart;
   endIndex += myStart;
   doRemove(startIndex, endIndex);
   afterChangedUpdate(event, LocalTimeCounter.currentTime());
 }
예제 #3
0
  public static PsiElement[] parsePattern(
      Project project,
      String context,
      String pattern,
      FileType fileType,
      Language language,
      String extension,
      boolean physical) {
    int offset = context.indexOf(PATTERN_PLACEHOLDER);

    final int patternLength = pattern.length();
    final String patternInContext = context.replace(PATTERN_PLACEHOLDER, pattern);

    final String ext = extension != null ? extension : fileType.getDefaultExtension();
    final String name = "__dummy." + ext;
    final PsiFileFactory factory = PsiFileFactory.getInstance(project);

    final PsiFile file =
        language == null
            ? factory.createFileFromText(
                name, fileType, patternInContext, LocalTimeCounter.currentTime(), physical, true)
            : factory.createFileFromText(name, language, patternInContext, physical, true);
    if (file == null) {
      return PsiElement.EMPTY_ARRAY;
    }

    final List<PsiElement> result = new ArrayList<PsiElement>();

    PsiElement element = file.findElementAt(offset);
    if (element == null) {
      return PsiElement.EMPTY_ARRAY;
    }

    PsiElement topElement = element;
    element = element.getParent();

    while (element != null) {
      if (element.getTextRange().getStartOffset() == offset
          && element.getTextLength() <= patternLength) {
        topElement = element;
      }
      element = element.getParent();
    }

    if (topElement instanceof PsiFile) {
      return topElement.getChildren();
    }

    final int endOffset = offset + patternLength;
    result.add(topElement);
    topElement = topElement.getNextSibling();

    while (topElement != null && topElement.getTextRange().getEndOffset() <= endOffset) {
      result.add(topElement);
      topElement = topElement.getNextSibling();
    }

    return result.toArray(new PsiElement[result.size()]);
  }
예제 #4
0
  public void insert(DocumentImpl subj, CharSequence s, int startIndex) {
    DocumentEvent event = beforeChangedUpdate(subj, startIndex, null, s, false);
    startIndex += myStart;
    doInsert(s, startIndex);

    afterChangedUpdate(event, LocalTimeCounter.currentTime());
    trimToSize(subj);
  }
예제 #5
0
 protected static PsiFile createLightFile(String fileName, String text)
     throws IncorrectOperationException {
   return PsiFileFactory.getInstance(getProject())
       .createFileFromText(
           fileName,
           FileTypeManager.getInstance().getFileTypeByFileName(fileName),
           text,
           LocalTimeCounter.currentTime(),
           false);
 }
예제 #6
0
 public static PsiFile createPseudoPhysicalFile(
     final Project project, final String fileName, final String text)
     throws IncorrectOperationException {
   return PsiFileFactory.getInstance(project)
       .createFileFromText(
           fileName,
           FileTypeManager.getInstance().getFileTypeByFileName(fileName),
           text,
           LocalTimeCounter.currentTime(),
           true);
 }
  @Nullable
  public static ClassLoader compileText(
      K2JVMCompileEnvironmentConfiguration configuration, String code) {
    configuration
        .getEnvironment()
        .addSources(
            new LightVirtualFile(
                "script" + LocalTimeCounter.currentTime() + ".kt", JetLanguage.INSTANCE, code));

    GenerationState generationState = analyzeAndGenerate(configuration);
    if (generationState == null) {
      return null;
    }
    return new GeneratedClassLoader(generationState.getFactory());
  }
  protected PsiFile createFromText(final Language language, CharSequence text, PsiManager manager) {
    @NonNls
    final LightVirtualFile virtualFile =
        new LightVirtualFile(
            "foo", createTemplateFakeFileType(language), text, LocalTimeCounter.currentTime());

    FileViewProvider viewProvider =
        new SingleRootFileViewProvider(manager, virtualFile, false) {
          @Override
          @NotNull
          public Language getBaseLanguage() {
            return language;
          }
        };

    return viewProvider.getPsi(language);
  }
예제 #9
0
 @NotNull
 private static XmlFile parseXmlFileInTemplate(
     String templateString, CustomTemplateCallback callback, boolean createPhysicalFile) {
   XmlFile xmlFile =
       (XmlFile)
           PsiFileFactory.getInstance(callback.getProject())
               .createFileFromText(
                   "dummy.xml",
                   StdFileTypes.XML,
                   templateString,
                   LocalTimeCounter.currentTime(),
                   createPhysicalFile);
   VirtualFile vFile = xmlFile.getVirtualFile();
   if (vFile != null) {
     vFile.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE);
   }
   return xmlFile;
 }
  private static Document createDocument(
      String value,
      @Nullable Language language,
      Project project,
      @NotNull SimpleDocumentCreator documentCreator) {
    if (language != null) {
      final PsiFileFactory factory = PsiFileFactory.getInstance(project);
      final FileType fileType = language.getAssociatedFileType();
      assert fileType != null;

      final long stamp = LocalTimeCounter.currentTime();
      final PsiFile psiFile =
          factory.createFileFromText(
              "Dummy." + fileType.getDefaultExtension(), fileType, value, stamp, true, false);
      documentCreator.customizePsiFile(psiFile);
      final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
      assert document != null;
      return document;
    } else {
      return EditorFactory.getInstance().createDocument(value);
    }
  }
예제 #11
0
 protected static PsiFile createPseudoPhysicalFile(String fileName, String text)
     throws IncorrectOperationException {
   FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(fileName);
   return PsiFileFactory.getInstance(getProject())
       .createFileFromText(fileName, fileType, text, LocalTimeCounter.currentTime(), true);
 }
 @Override
 public void rootChanged(PsiFile psiFile) {
   if (((PsiFileEx) psiFile).isContentsLoaded()) {
     setContent(new PsiFileContent((PsiFileImpl) psiFile, LocalTimeCounter.currentTime()));
   }
 }