Ejemplo n.º 1
0
 private boolean isTypeUsed(String strType) {
   String strRelativeName = TypePopup.getRelativeTypeName(strType);
   try {
     return _gsEditor.getParser().getTypeUsesMap().resolveType(strRelativeName) != null
         || TypeSystem.parseType(strRelativeName, _gsEditor.getParser().getTypeUsesMap().copy())
             != null;
   } catch (Exception e) {
     return false;
   }
 }
Ejemplo n.º 2
0
 private int findClasspathLocation() {
   List<IParseTree> locations = _gsEditor.getParser().getLocations();
   List<IClasspathStatement> listOut = new ArrayList<IClasspathStatement>();
   IParseTree.Search.getContainedParsedElementsByType(
       locations, IClasspathStatement.class, listOut);
   if (listOut.size() > 0) {
     return listOut.get(0).getLocation().getOffset();
   }
   return -1;
 }
Ejemplo n.º 3
0
 private int findUsesInsertionPosition(boolean bProgram) {
   Set usesStmts = _gsEditor.getParser().getTypeUsesMap().getUsesStatements();
   int iPos = -1;
   for (Iterator iterator = usesStmts.iterator(); iterator.hasNext(); ) {
     IUsesStatement usesStmt = (IUsesStatement) iterator.next();
     iPos = Math.max(usesStmt.getLocation().getOffset(), iPos);
   }
   if (iPos < 0) // No uses-stmts exists, check for a package-stmt
   {
     iPos = findPackageLocation();
     if (iPos < 0) {
       if (bProgram) {
         // No uses-stms or package-stmt, check for a classpath-stmt
         iPos = findClasspathLocation();
         if (iPos < 0) {
           // No uses-stmts, package-stmt, or classpath-stmt, insert the new uses-stmt at beginning
           // of source
           iPos = 0;
         } else {
           // Insert after the classpath-stmt
           iPos++;
         }
       } else {
         // No uses-stmts or package-stmt, insert the new uses-stmt at beginning of source
         iPos = 0;
       }
     } else {
       // Insert after the package-stmt, that's why we increment the position
       iPos++;
     }
   } else if (iPos == 0) // Implies there exists at least one uses-stmt
   {
     // Append the new uses-statement instead of inserting it (adding 1 ensures this)
     iPos = 1;
   }
   return iPos;
 }
Ejemplo n.º 4
0
  private void moveSelectionNow(boolean up) {
    int start = _gsEditor.getEditor().getSelectionStart();
    int end = _gsEditor.getEditor().getSelectionEnd();
    String script = _gsEditor.getEditor().getText();

    // If the caret is positioned at the first point in a new line, treat it as if it is on the
    // previous line only
    if (start != end && end > 0 && script.charAt(end - 1) == '\n') {
      end = end - 1;
    }

    // Handle raw whitespace moves
    int rawStart = TextComponentUtil.getLineStart(script, start);
    int rawEnd = TextComponentUtil.getLineEnd(script, end);
    String line = script.substring(rawStart, rawEnd);
    if (GosuStringUtil.isWhitespace(line) || line.trim().startsWith("//")) {
      if (up) {
        if (rawStart == 0) {
          return;
        }
        int lineStartBefore = TextComponentUtil.getLineStart(script, rawStart - 1);
        GosuRefactorUtil.MoveInstruction moveInstruction =
            new GosuRefactorUtil.MoveInstruction(false, false, lineStartBefore);
        handleMoveInstruction(script, moveInstruction, rawStart, rawEnd, up);
        return;
      } else {
        if (rawEnd >= script.length()) {
          return;
        }
        int lineStartBefore = rawEnd + 1;
        GosuRefactorUtil.MoveInstruction moveInstruction =
            new GosuRefactorUtil.MoveInstruction(false, false, lineStartBefore);
        handleMoveInstruction(script, moveInstruction, rawStart, rawEnd, up);
        return;
      }
    }

    int peStart = TextComponentUtil.findNonWhitespacePositionAfter(script, rawStart);
    int peEnd = TextComponentUtil.findNonWhitespacePositionBefore(script, rawEnd);

    peStart = Math.min(peStart, script.length() - 1);
    peEnd = Math.min(peEnd, script.length() - 1);

    // Find the first statement at the selection start
    IParseTree firstStatement =
        GosuRefactorUtil.findFirstStatementAtLine(
            TextComponentUtil.getLineAtPosition(_gsEditor.getEditor(), peStart),
            peStart,
            _gsEditor.getParser().getLocations());

    // Find the last statement at the selection end
    IParseTree lastStatement =
        GosuRefactorUtil.findLastStatementAtLine(
            TextComponentUtil.getLineAtPosition(_gsEditor.getEditor(), peEnd),
            peEnd,
            _gsEditor.getParser().getLocations());

    // Find the spanning range of those two statements
    IParseTree[] boundingPair =
        GosuRefactorUtil.findSpanningLogicalRange(firstStatement, lastStatement);

    // If a bounding pair exists, do the move
    if (boundingPair != null) {
      int clipStart =
          Math.min(rawStart, TextComponentUtil.getLineStart(script, boundingPair[0].getOffset()));
      int clipEnd =
          Math.max(rawEnd, TextComponentUtil.getLineEnd(script, boundingPair[1].getExtent()));

      // if this is not a class element (that is, we are in an impl), handle moving into a white
      // space line
      if (!GosuRefactorUtil.isClassElement(boundingPair[0])) {
        int lineStart =
            up
                ? TextComponentUtil.getWhiteSpaceOrCommentLineStartBefore(script, clipStart)
                : TextComponentUtil.getWhiteSpaceOrCommentLineStartAfter(script, clipEnd);
        if (lineStart != -1) {
          GosuRefactorUtil.MoveInstruction moveInstruction =
              new GosuRefactorUtil.MoveInstruction(false, false, lineStart);
          handleMoveInstruction(script, moveInstruction, clipStart, clipEnd, up);
          return;
        }
      }

      // Not handling an into whitespace move, so do a syntax aware move
      GosuRefactorUtil.MoveInstruction moveInstruction =
          up
              ? GosuRefactorUtil.getMoveUpInstruction(boundingPair[0])
              : GosuRefactorUtil.getMoveDownInstruction(boundingPair[1]);

      if (moveInstruction != null) {
        if (GosuRefactorUtil.isClassElement(boundingPair[1])) {
          IParseTree nextSibling = boundingPair[1].getNextSibling();
          if (nextSibling != null) {
            clipEnd = TextComponentUtil.getLineStart(script, nextSibling.getOffset()) - 1;
            if (!up) {
              moveInstruction.position =
                  TextComponentUtil.getDeepestWhiteSpaceLineStartAfter(
                      script, moveInstruction.position);
            }
          }
        }

        handleMoveInstruction(script, moveInstruction, clipStart, clipEnd, up);
      }
    }
  }