示例#1
0
 /**
  * Opens an editor on the given Dart element in the active page. Valid elements are all Dart
  * elements that are {@link SourceReference}. For elements inside a compilation unit, the parent
  * is opened in the editor is opened. If there already is an open Dart editor for the given
  * element, it is returned.
  *
  * @param element the input element; either a compilation unit ( <code>CompilationUnit</code>) or
  *     a class file ( <code>IClassFile</code>) or source references inside.
  * @param activate if set, the editor will be activated.
  * @param reveal if set, the element will be revealed.
  * @return returns the editor part of the opened editor or <code>null</code> if the element is not
  *     a {@link SourceReference} or the file was opened in an external editor.
  * @exception PartInitException if the editor could not be initialized or no workbench page is
  *     active
  * @exception DartModelException if this element does not exist or if an exception occurs while
  *     accessing its underlying resource
  */
 public static IEditorPart openInEditor(Element element, boolean activate, boolean reveal)
     throws DartModelException, PartInitException {
   IEditorPart part = EditorUtility.openInEditor(element, activate);
   if (reveal && part != null) {
     EditorUtility.revealInEditor(part, element);
   }
   return part;
 }
示例#2
0
 /**
  * Opens an editor on the given Dart element in the active page. Valid elements are all Dart
  * elements that are {@link SourceReference}. For elements inside a compilation unit, the parent
  * is opened in the editor is opened. If there already is an open Dart editor for the given
  * element, it is returned.
  *
  * @param element the input element; either a compilation unit ( <code>CompilationUnit</code>) or
  *     a class file ( <code>IClassFile</code>) or source references inside.
  * @param activate if set, the editor will be activated.
  * @param reveal if set, the element will be revealed.
  * @return returns the editor part of the opened editor or <code>null</code> if the element is not
  *     a {@link SourceReference} or the file was opened in an external editor.
  * @exception PartInitException if the editor could not be initialized or no workbench page is
  *     active
  * @exception DartModelException if this element does not exist or if an exception occurs while
  *     accessing its underlying resource
  */
 public static IEditorPart openInEditor(DartElement element, boolean activate, boolean reveal)
     throws DartModelException, PartInitException {
   if (!(element instanceof SourceReference)) {
     return null;
   }
   IEditorPart part = EditorUtility.openInEditor(element, activate);
   if (reveal && part != null) {
     EditorUtility.revealInEditor(part, element);
   }
   return part;
 }
示例#3
0
  @Override
  public void doRun(ISelection selection, Event event, UIInstrumentationBuilder instrumentation) {
    instrumentation.metric("command", command);

    if (!(selection instanceof ITextSelection)) {
      instrumentation.metric("Problem", "Selection was not a TextSelection");
    }

    IWorkbenchPage page = DartToolsPlugin.getActivePage();
    if (page == null) {
      instrumentation.metric("Problem", "Page was null");
      return;
    }

    IEditorPart part = page.getActiveEditor();
    if (part == null) {
      instrumentation.metric("Problem", "Part was null");
      return;
    }

    IEditorInput editorInput = part.getEditorInput();
    IProject project = EditorUtility.getProject(editorInput);
    instrumentation.data("Project", project.getName());
    savePubspecFile(project);
    runPubJob(project);
  }
  /*
   * Method declared in IAction.
   */
  @Override
  public final void run() {
    DartElement inputElement = EditorUtility.getEditorInputJavaElement(fEditor, false);
    if (!(inputElement instanceof SourceReference && inputElement.exists())) {
      return;
    }

    SourceReference source = (SourceReference) inputElement;
    SourceRange sourceRange;
    try {
      sourceRange = source.getSourceRange();
      if (sourceRange == null || sourceRange.getLength() == 0) {
        MessageDialog.openInformation(
            fEditor.getEditorSite().getShell(),
            SelectionActionMessages.StructureSelect_error_title,
            SelectionActionMessages.StructureSelect_error_message);
        return;
      }
    } catch (DartModelException e) {
    }
    ITextSelection selection = getTextSelection();
    SourceRange newRange = getNewSelectionRange(createSourceRange(selection), source);
    // Check if new selection differs from current selection
    if (selection.getOffset() == newRange.getOffset()
        && selection.getLength() == newRange.getLength()) {
      return;
    }
    fSelectionHistory.remember(newSourceRange(selection.getOffset(), selection.getLength()));
    try {
      fSelectionHistory.ignoreSelectionChanges();
      fEditor.selectAndReveal(newRange.getOffset(), newRange.getLength());
    } finally {
      fSelectionHistory.listenToSelectionChanges();
    }
  }
  /**
   * Opens the editor currently associated with the given element (DartElement, IFile, IStorage...)
   *
   * @return an open editor or <code>null</code> if an external editor was opened
   * @throws PartInitException if the editor could not be opened or the input element is not valid
   */
  public static IEditorPart openInEditor(Object inputElement, boolean activate)
      throws DartModelException, PartInitException {

    if (inputElement instanceof IFile) {
      return openInEditor((IFile) inputElement, activate);
    }
    DartX.todo();
    if (inputElement instanceof ImportElement) {
      inputElement = ((ImportElement) inputElement).getImportedLibrary();
    }
    if (inputElement instanceof Element) {
      CompilationUnitElement cu = getCompilationUnit((Element) inputElement);
      IWorkbenchPage page = DartToolsPlugin.getActivePage();
      if (cu != null && page != null) {
        IEditorPart editor = page.getActiveEditor();
        if (editor != null) {
          Element editorCU = EditorUtility.getEditorInputDartElement2(editor, false);
          if (cu.equals(editorCU)) {
            if (activate && page.getActivePart() != editor) {
              page.activate(editor);
            }
            return editor;
          }
        }
      }
    }

    IEditorInput input = getEditorInput(inputElement);

    if (input == null) {
      return null;
    }

    return openInEditor(input, getEditorID(input), activate);
  }
示例#6
0
  private void savePubspecFile(IContainer container) {
    IResource resource = container.findMember(DartCore.PUBSPEC_FILE_NAME);
    if (resource != null) {
      IEditorPart editor = EditorUtility.isOpenInEditor(resource);
      if (editor != null && editor.isDirty()) {
        if (MessageDialog.openQuestion(
            getShell(),
            NLS.bind(ActionMessages.RunPubAction_commandText, command),
            ActionMessages.RunPubAction_savePubspecMessage)) {

          editor.doSave(new NullProgressMonitor());
        }
      }
    }
  }
 @Override
 public void run(IMarker marker) {
   try {
     IEditorPart part = EditorUtility.isOpenInEditor(unit);
     if (part == null) {
       part = DartUI.openInEditor(unit, true, false);
       if (part instanceof ITextEditor) {
         ((ITextEditor) part).selectAndReveal(offset, length);
       }
     }
     if (part != null) {
       IEditorInput input = part.getEditorInput();
       IDocument doc = getDocumentProvider().getDocument(input);
       proposal.apply(doc);
     }
   } catch (CoreException e) {
     DartToolsPlugin.log(e);
   }
 }
  private static IMarkerResolution[] internalGetResolutions(IMarker marker) {
    if (!internalHasResolutions(marker)) {
      return NO_RESOLUTIONS;
    }

    CompilationUnit cu = getCompilationUnit(marker);
    if (cu != null) {
      IEditorInput input = EditorUtility.getEditorInput(cu);
      if (input != null) {
        IProblemLocation location = findProblemLocation(input, marker);
        if (location != null) {

          IInvocationContext context =
              new AssistContext(cu, location.getOffset(), location.getLength());

          // TODO(scheglov) do we need this?
          //          if (!hasProblem(context.getASTRoot().getProblems(), location)) {
          //            return NO_RESOLUTIONS;
          //          }

          List<IDartCompletionProposal> proposals = Lists.newArrayList();
          DartCorrectionProcessor_OLD.collectCorrections(
              context, new IProblemLocation[] {location}, proposals);
          Collections.sort(proposals, new CompletionProposalComparator());

          int nProposals = proposals.size();
          IMarkerResolution[] resolutions = new IMarkerResolution[nProposals];
          for (int i = 0; i < nProposals; i++) {
            resolutions[i] =
                new CorrectionMarkerResolution(
                    cu, location.getOffset(), location.getLength(), proposals.get(i));
          }
          return resolutions;
        }
      }
    }
    return NO_RESOLUTIONS;
  }
 private CompilationUnit getCompilationUnit() {
   return (CompilationUnit) EditorUtility.getEditorInputDartElement(fEditor, false);
 }
示例#10
0
 /**
  * Reveals the given Dart element in the given editor. If the element is not an instance of <code>
  * SourceReference</code> this method result in a NOP. If it is a source reference no checking is
  * done if the editor displays a compilation unit or class file that contains the source reference
  * element. The editor simply reveals the source range denoted by the given element.
  *
  * @param part the editor displaying a compilation unit or class file
  * @param element the element to be revealed
  */
 public static void revealInEditor(IEditorPart part, DartElement element) {
   EditorUtility.revealInEditor(part, element);
 }