Example #1
0
 static TextChange createEditorChange(CeylonEditor editor, IDocument document) {
   if (editor.isDirty()) {
     return new DocumentChange("Move from Source File", document);
   } else {
     return new TextFileChange("Move from Source File", getFile(editor.getEditorInput()));
   }
 }
 public Declaration select(List<Declaration> proposals) {
   CeylonEditor editor = (CeylonEditor) getCurrentEditor();
   ImportSelectionDialog fid = new ImportSelectionDialog(editor.getSite().getShell(), proposals);
   if (fid.open() == Window.OK) {
     return (Declaration) fid.getFirstResult();
   }
   return null;
 }
 private void go(Tree.Declaration dec) {
   CeylonEditor editor = (CeylonEditor) getCurrentEditor();
   if (dec != null) {
     IPath path =
         getNodePath(
             dec,
             editor.getParseController().getProject(),
             editor.getParseController().getTypeChecker());
     gotoLocation(path, getStartOffset(dec));
   }
 }
 static Tree.CompilationUnit getRootNode(PhasedUnit unit) {
   IEditorPart ce = getCurrentEditor();
   if (ce instanceof CeylonEditor) {
     CeylonEditor editor = (CeylonEditor) ce;
     CeylonParseController cpc = editor.getParseController();
     if (cpc != null) {
       Tree.CompilationUnit rn = cpc.getTypecheckedRootNode();
       if (rn != null) {
         Unit u = rn.getUnit();
         if (u.equals(unit.getUnit())) {
           return rn;
         }
       }
     }
   }
   return unit.getCompilationUnit();
 }
Example #5
0
 public static String getDeclarationName(CeylonEditor editor) {
   Node node = editor.getSelectedNode();
   if (node instanceof Tree.Declaration) {
     return ((Tree.Declaration) node).getIdentifier().getText();
   } else {
     return null;
   }
 }
Example #6
0
 public static boolean canMoveDeclaration(CeylonEditor editor) {
   Node node = editor.getSelectedNode();
   if (node instanceof Tree.Declaration) {
     Declaration d = ((Tree.Declaration) node).getDeclarationModel();
     return d != null && d.isToplevel();
   } else {
     return false;
   }
 }
 private Tree.Declaration getSelectionTarget(ITextSelection textSel) {
   CeylonEditor editor = (CeylonEditor) getCurrentEditor();
   CeylonParseController pc = editor.getParseController();
   if (pc == null) {
     return null;
   } else {
     Tree.CompilationUnit ast = pc.getRootNode();
     if (ast == null) {
       return null;
     } else {
       Object sourceNode = findNode(ast, textSel.getOffset());
       if (sourceNode == null) {
         return null;
       } else {
         return getReferencedNode(sourceNode, pc);
       }
     }
   }
 }
 @Override
 public Object execute(ExecutionEvent event) throws ExecutionException {
   CeylonEditor editor = (CeylonEditor) getCurrentEditor();
   Tree.CompilationUnit cu = editor.getParseController().getRootNode();
   if (cu != null) {
     String imports = imports(cu);
     if (imports != null) {
       TextFileChange tfc =
           new TextFileChange(
               "Clean Imports", ((IFileEditorInput) editor.getEditorInput()).getFile());
       tfc.setEdit(new MultiTextEdit());
       ImportList il = cu.getImportList();
       int start;
       int length;
       String extra;
       if (il == null || il.getImports().isEmpty()) {
         start = 0;
         length = 0;
         extra = "\n";
       } else {
         start = il.getStartIndex();
         length = il.getStopIndex() - il.getStartIndex() + 1;
         extra = "";
       }
       //                if (!imports.trim().isEmpty()) {
       tfc.addEdit(new ReplaceEdit(start, length, imports + extra));
       tfc.initializeValidationData(null);
       try {
         getWorkspace().run(new PerformChangeOperation(tfc), new NullProgressMonitor());
       } catch (CoreException ce) {
         throw new ExecutionException("Error cleaning imports", ce);
       }
       //                }
     }
   }
   return null;
 }
 @Override
 public Object execute(ExecutionEvent event) throws ExecutionException {
   IEditorPart editor = getCurrentEditor();
   if (editor instanceof CeylonEditor) {
     CeylonEditor ce = (CeylonEditor) editor;
     Tree.CompilationUnit rootNode = ce.getParseController().getRootNode();
     if (rootNode != null) {
       IRegion selection = ce.getSelection();
       int start = selection.getOffset();
       int end = start + selection.getLength();
       Node node = findNode(rootNode, start, end);
       List<ICompletionProposal> list = new ArrayList<ICompletionProposal>();
       addPrintProposal(rootNode, list, node, start);
       if (!list.isEmpty()) {
         IDocument doc = ce.getCeylonSourceViewer().getDocument();
         ICompletionProposal proposal = list.get(0);
         proposal.apply(doc);
         Point point = proposal.getSelection(doc);
         ce.getSelectionProvider().setSelection(new TextSelection(point.x, point.y));
       }
     }
   }
   return null;
 }
 static void addRemoveAliasProposal(
     Tree.ImportMemberOrType imt,
     Collection<ICompletionProposal> proposals,
     IFile file,
     CeylonEditor editor) {
   if (imt != null) {
     Declaration dec = imt.getDeclarationModel();
     Tree.CompilationUnit upToDateAndTypechecked =
         editor.getParseController().getTypecheckedRootNode();
     if (dec != null && imt.getAlias() != null && upToDateAndTypechecked != null) {
       TextFileChange change = new TextFileChange("Remove Alias", file);
       change.setEdit(new MultiTextEdit());
       Tree.Identifier aid = imt.getAlias().getIdentifier();
       change.addEdit(
           new DeleteEdit(
               aid.getStartIndex(), imt.getIdentifier().getStartIndex() - aid.getStartIndex()));
       upToDateAndTypechecked.visit(new AliasRemovalVisitor(dec, change, aid));
       proposals.add(new RemoveAliasProposal(file, dec, change));
     }
   }
 }
 public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int offset) {
   try {
     if (lastOffset >= 0 && offset > 0 && !isIdentifierCharacter(viewer, offset)) {
       // user typed a whitespace char with an open
       // completions window, so close the window
       return NO_COMPLETIONS;
     }
   } catch (BadLocationException e1) {
     e1.printStackTrace();
     return NO_COMPLETIONS;
   }
   if (offset == lastOffset) {
     filter = !filter;
   }
   lastOffset = offset;
   try {
     return contentProposer.getContentProposals(
         editor.getParseController(), offset, viewer, filter);
   } catch (Exception e) {
     e.printStackTrace();
     return NO_COMPLETIONS;
   }
 }
 @Override
 public void init(IWorkbench workbench) {
   setPreferenceStore(getPreferences());
   CeylonEditor.initializeBrackMatcherPreferences();
 }
 public static void refineFormalMembers(CeylonEditor editor) throws ExecutionException {
   Tree.CompilationUnit cu = editor.getParseController().getRootNode();
   if (cu == null) return;
   Node node = getSelectedNode(editor);
   IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
   final TextChange change = new DocumentChange("Refine Formal Members", document);
   change.setEdit(new MultiTextEdit());
   // TODO: copy/pasted from CeylonQuickFixAssistant
   Tree.Body body;
   int offset;
   if (node instanceof Tree.ClassDefinition) {
     body = ((Tree.ClassDefinition) node).getClassBody();
     offset = -1;
   } else if (node instanceof Tree.InterfaceDefinition) {
     body = ((Tree.InterfaceDefinition) node).getInterfaceBody();
     offset = -1;
   } else if (node instanceof Tree.ObjectDefinition) {
     body = ((Tree.ObjectDefinition) node).getClassBody();
     offset = -1;
   } else if (node instanceof Tree.ClassBody || node instanceof Tree.InterfaceBody) {
     body = (Tree.Body) node;
     offset = editor.getSelection().getOffset();
   } else {
     // TODO run a visitor to find the containing body!
     return; // TODO popup error dialog
   }
   boolean isInterface = body instanceof Tree.InterfaceBody;
   // TODO: copy/pasted from ImplementFormalMembersProposal
   List<Statement> statements = body.getStatements();
   String indent;
   String bodyIndent = getIndent(body, document);
   if (statements.isEmpty()) {
     indent = "\n" + bodyIndent + getDefaultIndent();
     if (offset < 0) offset = body.getStartIndex() + 1;
   } else {
     Statement statement = statements.get(statements.size() - 1);
     indent = "\n" + getIndent(statement, document);
     if (offset < 0) offset = statement.getStopIndex() + 1;
   }
   StringBuilder result = new StringBuilder();
   Set<Declaration> already = new HashSet<Declaration>();
   for (DeclarationWithProximity dwp : getProposals(node, cu).values()) {
     Declaration d = dwp.getDeclaration();
     if (d.isFormal() && ((ClassOrInterface) node.getScope()).isInheritedFromSupertype(d)) {
       ProducedReference pr = getRefinedProducedReference(node, d);
       result
           .append(indent)
           .append(getRefinementTextFor(d, pr, isInterface, indent))
           .append(indent);
       importSignatureTypes(d, cu, already);
     }
   }
   try {
     if (document.getChar(offset) == '}' && result.length() > 0) {
       result.append("\n").append(bodyIndent);
     }
   } catch (BadLocationException e) {
     e.printStackTrace();
   }
   applyImports(change, already, cu);
   change.addEdit(new InsertEdit(offset, result.toString()));
   change.initializeValidationData(null);
   try {
     getWorkspace().run(new PerformChangeOperation(change), new NullProgressMonitor());
   } catch (CoreException ce) {
     throw new ExecutionException("Error cleaning imports", ce);
   }
 }
  @Deprecated
  // replaced by RefineFormalMembersQuickFix.ceylon
  private void refineFormalMembers(IDocument document) throws ExecutionException {
    if (rootNode == null) return;
    TextChange change = new DocumentChange("Refine Members", document);
    change.setEdit(new MultiTextEdit());
    // TODO: copy/pasted from CeylonQuickFixAssistant
    Tree.Body body;
    int offset;
    if (node instanceof Tree.ClassDefinition) {
      ClassDefinition classDefinition = (Tree.ClassDefinition) node;
      body = classDefinition.getClassBody();
      offset = -1;
    } else if (node instanceof Tree.InterfaceDefinition) {
      Tree.InterfaceDefinition interfaceDefinition = (Tree.InterfaceDefinition) node;
      body = interfaceDefinition.getInterfaceBody();
      offset = -1;
    } else if (node instanceof Tree.ObjectDefinition) {
      Tree.ObjectDefinition objectDefinition = (Tree.ObjectDefinition) node;
      body = objectDefinition.getClassBody();
      offset = -1;
    } else if (node instanceof Tree.ObjectExpression) {
      Tree.ObjectExpression objectExpression = (Tree.ObjectExpression) node;
      body = objectExpression.getClassBody();
      offset = -1;
    } else if (node instanceof Tree.ClassBody || node instanceof Tree.InterfaceBody) {
      body = (Tree.Body) node;
      IEditorPart editor = getCurrentEditor();
      if (editor instanceof CeylonEditor) {
        CeylonEditor ce = (CeylonEditor) editor;
        offset = ce.getSelection().getOffset();
      } else {
        offset = -1;
      }
    } else {
      return;
    }
    if (body == null) {
      return;
    }
    boolean isInterface = body instanceof Tree.InterfaceBody;
    List<Tree.Statement> statements = body.getStatements();
    String indent;
    //        String bodyIndent = getIndent(body, document);
    String bodyIndent = utilJ2C().indents().getIndent(node, document);
    String delim = utilJ2C().indents().getDefaultLineDelimiter(document);
    if (statements.isEmpty()) {
      indent = delim + bodyIndent + utilJ2C().indents().getDefaultIndent();
      if (offset < 0) {
        offset = body.getStartIndex() + 1;
      }
    } else {
      Tree.Statement statement = statements.get(statements.size() - 1);
      indent = delim + utilJ2C().indents().getIndent(statement, document);
      if (offset < 0) {
        offset = statement.getEndIndex();
      }
    }
    StringBuilder result = new StringBuilder();
    Set<Declaration> already = new HashSet<Declaration>();
    ClassOrInterface ci = (ClassOrInterface) node.getScope();
    Unit unit = node.getUnit();
    Set<String> ambiguousNames = new HashSet<String>();
    // TODO: does not return unrefined overloaded
    //      versions of a method with one overlaad
    //      already refined
    Collection<DeclarationWithProximity> proposals =
        ci.getMatchingMemberDeclarations(unit, ci, "", 0).values();
    for (DeclarationWithProximity dwp : proposals) {
      Declaration dec = dwp.getDeclaration();
      for (Declaration d : overloads(dec)) {
        try {
          if (d.isFormal() && ci.isInheritedFromSupertype(d)) {
            appendRefinementText(isInterface, indent, result, ci, unit, d);
            importProposals().importSignatureTypes(d, rootNode, already);
            ambiguousNames.add(d.getName());
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
    for (TypeDeclaration superType : ci.getSupertypeDeclarations()) {
      for (Declaration m : superType.getMembers()) {
        try {
          if (m.getName() != null && m.isShared()) {
            Declaration r = ci.getMember(m.getName(), null, false);
            if ((r == null || !r.refines(m) && !r.getContainer().equals(ci))
                && ambiguousNames.add(m.getName())) {
              appendRefinementText(isInterface, indent, result, ci, unit, m);
              importProposals().importSignatureTypes(m, rootNode, already);
            }
          }
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }

    try {
      if (document.getChar(offset) == '}' && result.length() > 0) {
        result.append(delim).append(bodyIndent);
      }
    } catch (BadLocationException e) {
      e.printStackTrace();
    }
    importProposals().applyImports(change, already, rootNode, document);
    change.addEdit(new InsertEdit(offset, result.toString()));
    change.initializeValidationData(null);
    try {
      getWorkspace().run(new PerformChangeOperation(change), new NullProgressMonitor());
    } catch (CoreException ce) {
      ce.printStackTrace();
    }
  }
 public OutlinePopup(CeylonEditor editor, Shell shell, int shellStyle) {
   super(shell, shellStyle, PLUGIN_ID + ".editor.showOutline", editor);
   setTitleText("Quick Outline \u2014 " + editor.getEditorInput().getName());
 }