Esempio n. 1
0
  public void removeTrailingWhitespace() {
    int end = textArea.getDocument().getLength();

    // remove trailing empty lines
    int realEnd = end;
    if (eolAt(end - 1)) {
      while (eolAt(end - 2) || getText(end - 2, end - 1).equals("\r")) end--;
      if (end < realEnd) textArea.replaceRange("", end - 1, realEnd - 1);
    }

    // remove trailing white space from each line
    for (int i = textArea.getLineCount() - 1; i >= 0; i--)
      try {
        int start = textArea.getLineStartOffset(i);
        if (eolAt(end - 1)) end--;
        if (start == end) continue;
        String line = getText(start, end);
        realEnd = end;
        while (start < end && Character.isWhitespace(line.charAt(end - start - 1))) end--;
        if (end < realEnd) textArea.replaceRange("", end, realEnd);
        end = start;
      } catch (BadLocationException e) {
        /* cannot happen */
      }
  }
Esempio n. 2
0
  public void addImport(String className) {
    List<Import> imports = getImports();

    if (imports.size() == 0) {
      TokenIterator iter = new TokenIterator();
      int offset = 0;
      boolean insertLF = false;
      while (iter.hasNext()) {
        Token token = iter.next();
        if (token.type != token.RESERVED_WORD) {
          insertLF = false;
          continue;
        }
        if (getText(token).equals("package")) {
          skipToEOL(iter, token);
          insertLF = true;
        } else {
          offset = token.offset;
          break;
        }
      }
      textArea.insert((insertLF ? "\n" : "") + "import " + className + ";\n\n", offset);
      return;
    }

    String string = "import " + className + ";\n";
    Import imp = new Import(0, 0, className);
    int insert = -1;

    for (int i = 0; i < imports.size(); i++) {
      int cmp = imports.get(i).compareTo(imp);
      if (cmp == 0) return;
      if (cmp < 0) insert = i;
    }

    // 'insert' is the index of the import _after_ which we
    // want to insert the current import.
    if (insert >= 0) string = "\n" + string;
    if (insert >= 0 && !imp.getPackage().equals(imports.get(insert).getPackage()))
      string = "\n" + string;
    if (insert + 1 < imports.size()
        && !imp.getPackage().equals(imports.get(insert + 1).getPackage())) string += "\n";
    int startOffset = insert < 0 ? imports.get(0).startOffset : imports.get(insert).endOffset;
    int endOffset =
        insert + 1 < imports.size()
            ? imports.get(insert + 1).startOffset
            : imports.get(insert).endOffset;
    textArea.replaceRange(string, startOffset, endOffset);
  }
Esempio n. 3
0
  public void sortImports() {
    List<Import> imports = getImports();
    if (imports.size() == 0) return;
    int start = imports.get(0).startOffset;
    while (emptyLineAt(start - 2)) start--;
    int end = imports.get(imports.size() - 1).endOffset;
    while (eolAt(end)) end++;

    Collections.sort(
        imports,
        new Comparator<Import>() {
          public int compare(Import i1, Import i2) {
            return i1.classOrPackage.compareTo(i2.classOrPackage);
          }

          public boolean equals(Object o) {
            return false;
          }
        });

    StringBuffer buffer = new StringBuffer();
    String lastPrefix = null;
    String lastImport = null;
    for (Import imp : imports) {
      if (imp.classOrPackage.equals(lastImport)) continue;
      lastImport = imp.classOrPackage;

      String prefix = imp.getPackage();
      if (!prefix.equals(lastPrefix)) {
        buffer.append("\n");
        lastPrefix = prefix;
      }
      // TODO: honor comments
      buffer.append(getText(imp.startOffset, imp.endOffset));
      buffer.append("\n");
    }
    buffer.append("\n");

    textArea.replaceRange(buffer.toString(), start, end);
  }
Esempio n. 4
0
 void removeImport(Import imp) {
   int start = imp.startOffset, end = imp.endOffset;
   if (emptyLineAt(start - 2) && emptyLineAt(end)) end += 2;
   else if (eolAt(end)) end++;
   textArea.replaceRange("", start, end);
 }