コード例 #1
0
 private void addLibsToClasspath() {
   try {
     List<File> libDirectories = ProjectUtils.getLibDirectories(javaProject);
     for (File libDirectory : libDirectories) {
       addToClasspath(libDirectory);
     }
   } catch (JavaModelException e) {
     KotlinLogger.logAndThrow(e);
   } catch (CoreException e) {
     KotlinLogger.logAndThrow(e);
   }
 }
コード例 #2
0
  private void addSourcesToClasspath() {
    try {
      for (File srcDirectory : ProjectUtils.getSrcDirectories(javaProject)) {
        addToClasspath(srcDirectory);
      }

      for (File srcDirectory : ProjectUtils.collectDependenciesClasspath(javaProject)) {
        addToClasspath(srcDirectory);
      }
    } catch (JavaModelException e) {
      KotlinLogger.logAndThrow(e);
    } catch (CoreException e) {
      KotlinLogger.logAndThrow(e);
    }
  }
コード例 #3
0
  private void addJreClasspath() {
    try {
      IRuntimeClasspathEntry computeJREEntry = JavaRuntime.computeJREEntry(javaProject);
      if (computeJREEntry == null) {
        return;
      }

      IRuntimeClasspathEntry[] jreEntries =
          JavaRuntime.resolveRuntimeClasspathEntry(computeJREEntry, javaProject);

      if (jreEntries.length != 0) {
        for (IRuntimeClasspathEntry jreEntry : jreEntries) {
          addToClasspath(jreEntry.getClasspathEntry().getPath().toFile());
        }

        return;
      }
    } catch (JavaModelException e) {
      KotlinLogger.logAndThrow(e);
    } catch (CoreException e) {
      KotlinLogger.logAndThrow(e);
    }
  }
コード例 #4
0
  private int computeIndentCount(IDocument document, int offset) {
    try {
      if (offset == document.getLength()) {
        return 0;
      }

      IFile file = EditorUtil.getFile(editor);
      if (file == null) {
        KotlinLogger.logError("Failed to retrieve IFile from editor " + editor, null);
        return 0;
      }

      if (document.get().contains(LineEndUtil.CARRIAGE_RETURN_STRING)) {
        offset -= document.getLineOfOffset(offset);
      }

      PsiFile parsedDocument = KotlinPsiManager.getKotlinFileIfExist(file, document.get());
      if (parsedDocument == null) {
        return 0;
      }

      PsiElement leaf = parsedDocument.findElementAt(offset);
      if (leaf == null) {
        return 0;
      }

      if (leaf.getNode().getElementType() != JetTokens.WHITE_SPACE) {
        leaf = parsedDocument.findElementAt(offset - 1);
      }

      int indent = 0;

      ASTNode node = null;
      if (leaf != null) {
        node = leaf.getNode();
      }
      while (node != null) {
        indent = AlignmentStrategy.updateIndent(node, indent);
        node = node.getTreeParent();
      }

      return indent;
    } catch (BadLocationException e) {
      KotlinLogger.logAndThrow(e);
    }

    return 0;
  }
コード例 #5
0
  private static boolean isNewLineBefore(IDocument document, int offset) {
    try {
      offset--;
      char prev = IndenterUtil.SPACE_CHAR;
      StringBuilder bufBefore = new StringBuilder(prev);
      while (IndenterUtil.isWhiteSpaceChar(prev) && offset > 0) {
        prev = document.getChar(offset--);
        bufBefore.append(prev);
      }

      return containsNewLine(document, bufBefore.toString());
    } catch (BadLocationException e) {
      KotlinLogger.logAndThrow(e);
    }

    return false;
  }
コード例 #6
0
  private void autoEditBeforeCloseBrace(IDocument document, DocumentCommand command) {
    if (isNewLineBefore(document, command.offset)) {
      try {
        int spaceLength =
            command.offset - findEndOfWhiteSpaceBefore(document, command.offset - 1, 0) - 1;

        command.text =
            IndenterUtil.createWhiteSpace(
                    computeIndentCount(document, command.offset) - 1,
                    0,
                    TextUtilities.getDefaultLineDelimiter(document))
                + CLOSING_BRACE_STRING;
        command.offset -= spaceLength;
        document.replace(command.offset, spaceLength, "");
      } catch (BadLocationException e) {
        KotlinLogger.logAndThrow(e);
      }
    }
  }
コード例 #7
0
  private void autoEditAfterNewLine(IDocument document, DocumentCommand command) {
    if (command.offset == -1 || document.getLength() == 0) {
      return;
    }

    try {
      int p = command.offset == document.getLength() ? command.offset - 1 : command.offset;
      IRegion info = document.getLineInformationOfOffset(p);
      int start = info.getOffset();

      StringBuffer buf = new StringBuffer(command.text);

      int end = findEndOfWhiteSpaceAfter(document, start, command.offset);

      String lineSpaces = (end > start) ? document.get(start, end - start) : "";
      buf.append(lineSpaces);

      if (isAfterOpenBrace(document, command.offset - 1, start)) {
        buf.append(
            IndenterUtil.createWhiteSpace(1, 0, TextUtilities.getDefaultLineDelimiter(document)));

        if (isBeforeCloseBrace(document, command.offset, info.getOffset() + info.getLength())) {
          command.shiftsCaret = false;
          command.caretOffset = command.offset + buf.length();

          buf.append(command.text);
          buf.append(lineSpaces);
        }
        command.text = buf.toString();
      } else {
        int indent = computeIndentCount(document, command.offset);
        if (isBeforeCloseBrace(document, command.offset, info.getOffset() + info.getLength())) {
          indent--;
        }
        command.text +=
            IndenterUtil.createWhiteSpace(
                indent, 0, TextUtilities.getDefaultLineDelimiter(document));
      }
    } catch (BadLocationException e) {
      KotlinLogger.logAndThrow(e);
    }
  }
  @NotNull
  private List<IType> findAllTypes(@NotNull String typeName) {
    IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
    List<IType> searchCollector = new ArrayList<IType>();
    TypeNameMatchRequestor requestor = new KotlinSearchTypeRequestor(searchCollector);
    try {
      searchEngine.searchAllTypeNames(
          null,
          SearchPattern.R_EXACT_MATCH,
          typeName.toCharArray(),
          SearchPattern.R_EXACT_MATCH,
          IJavaSearchConstants.TYPE,
          scope,
          requestor,
          IJavaSearchConstants.WAIT_UNTIL_READY_TO_SEARCH,
          null);
    } catch (CoreException e) {
      KotlinLogger.logAndThrow(e);
    }

    return searchCollector;
  }
コード例 #9
0
  @Override
  protected void performTest(String fileText, String expected) {
    IFile file = testEditor.getEditingFile();

    Character typedCharacter = TypingUtils.typedCharacter(fileText);
    if (typedCharacter != null) {
      testEditor.type(typedCharacter);
    }

    for (DiagnosticAnnotation annotation :
        DiagnosticAnnotationUtil.INSTANCE.createParsingDiagnosticAnnotations(file)) {
      AnnotationManager.addProblemMarker(annotation, file);
    }

    try {
      IMarker[] markers =
          testEditor.getEditingFile().findMarkers(IMarker.PROBLEM, true, IResource.DEPTH_INFINITE);
      String actual = insertTagsForErrors(EditorUtil.getSourceCode(getEditor()), markers);

      EditorTestUtils.assertByStringWithOffset(actual, expected);
    } catch (CoreException e) {
      KotlinLogger.logAndThrow(e);
    }
  }