/**
  * Returns the variable and function names at the current line, or <code>null</code> if none.
  *
  * @param part text editor
  * @param selection text selection
  * @return the variable and function names at the current line, or <code>null</code> if none. The
  *     array has two elements, the first is the variable name, the second is the function name.
  */
 protected String[] getVariableAndFunctionName(IWorkbenchPart part, ISelection selection) {
   ITextEditor editor = getEditor(part);
   if (editor != null && selection instanceof ITextSelection) {
     ITextSelection textSelection = (ITextSelection) selection;
     IDocumentProvider documentProvider = editor.getDocumentProvider();
     try {
       documentProvider.connect(this);
       IDocument document = documentProvider.getDocument(editor.getEditorInput());
       IRegion region = document.getLineInformationOfOffset(textSelection.getOffset());
       String string = document.get(region.getOffset(), region.getLength()).trim();
       if (string.startsWith("var ")) { // $NON-NLS-1$
         String varName = string.substring(4).trim();
         String fcnName =
             getFunctionName(
                 document, varName, document.getLineOfOffset(textSelection.getOffset()));
         return new String[] {varName, fcnName};
       }
     } catch (CoreException e) {
     } catch (BadLocationException e) {
     } finally {
       documentProvider.disconnect(this);
     }
   }
   return null;
 }
 private IDocument getDocument(IDocumentProvider provider, IEditorInput input) {
   if (input == null) {
     return null;
   }
   IDocument result = null;
   try {
     provider.connect(input);
     result = provider.getDocument(input);
   } catch (CoreException e) {
   } finally {
     provider.disconnect(input);
   }
   return result;
 }
示例#3
0
  /**
   * Disposes of the connection with the document provider. Subclasses may extend.
   *
   * @since 3.0
   */
  protected void disposeDocumentProvider() {
    IDocumentProvider provider = getDocumentProvider();
    if (provider != null) {

      IEditorInput input = getEditorInput();
      if (input != null) provider.disconnect(input);

      if (fElementStateListener != null) {
        provider.removeElementStateListener(fElementStateListener);
        fElementStateListener = null;
      }
    }
    fImplicitDocumentProvider = null;
  }
示例#4
0
  protected void fireActivity(TextEditActivity activity) {

    expectedActivities.add(activity);

    List<ITextOperation> textOps = activity.toOperation().getTextOperations();

    IFile file = ((EclipseFileImpl) currentActiveEditor.getFile()).getDelegate();

    FileEditorInput input = new FileEditorInput(file);
    IDocumentProvider provider = EditorManager.getDocumentProvider(input);

    try {
      provider.connect(input);
    } catch (CoreException e) {
      log.error("Could not connect to a document provider on file '" + file.toString() + "':", e);
      return;
    }

    try {
      IDocument doc = provider.getDocument(input);
      if (doc == null) {
        log.error(
            "Could not connect to a document provider on file '" + file.toString() + "':",
            new StackTrace());
        return;
      }

      for (ITextOperation textOp : textOps) {
        try {
          if (textOp instanceof DeleteOperation)
            doc.replace(textOp.getPosition(), textOp.getTextLength(), "");
          if (textOp instanceof InsertOperation)
            doc.replace(textOp.getPosition(), 0, textOp.getText());
        } catch (BadLocationException e) {
          log.error("Invalid location for " + textOp);
        }
      }
    } finally {
      provider.disconnect(input);
    }
  }
  public static void checkRegions(
      IProject project,
      String fileName,
      List<TestRegion> regionList,
      AbstractHyperlinkDetector elDetector)
      throws Exception {
    IFile file = project.getFile(fileName);

    assertNotNull("The file \"" + fileName + "\" is not found", file);
    assertTrue("The file \"" + fileName + "\" is not found", file.isAccessible());

    FileEditorInput editorInput = new FileEditorInput(file);

    IDocumentProvider documentProvider = null;
    try {
      documentProvider = DocumentProviderRegistry.getDefault().getDocumentProvider(editorInput);
    } catch (Exception x) {
      x.printStackTrace();
      fail("An exception caught: " + x.getMessage());
    }

    assertNotNull(
        "The document provider for the file \"" + fileName + "\" is not loaded", documentProvider);

    try {
      documentProvider.connect(editorInput);
    } catch (Exception x) {
      x.printStackTrace();
      fail(
          "The document provider is not able to be initialized with the editor input\nAn exception caught: "
              + x.getMessage());
    }

    IDocument document = documentProvider.getDocument(editorInput);

    assertNotNull("The document for the file \"" + fileName + "\" is not loaded", document);

    if (regionList.get(0).region == null) loadRegions(regionList, document);

    int expected = 0;
    for (TestRegion testRegion : regionList) expected += testRegion.region.getLength() + 1;

    IEditorPart part = openFileInEditor(file);
    ISourceViewer viewer = null;
    if (part instanceof JavaEditor) {
      viewer = ((JavaEditor) part).getViewer();
      elDetector.setContext(new TestContext((ITextEditor) part));
    } else if (part instanceof EditorPartWrapper) {
      if (((EditorPartWrapper) part).getEditor() instanceof WebCompoundEditor) {
        WebCompoundEditor wce = (WebCompoundEditor) ((EditorPartWrapper) part).getEditor();
        viewer = wce.getSourceEditor().getTextViewer();
        elDetector.setContext(new TestContext(wce.getSourceEditor()));
      } else if (((EditorPartWrapper) part).getEditor() instanceof XMLTextEditorStandAlone) {
        XMLTextEditorStandAlone xtesa =
            (XMLTextEditorStandAlone) ((EditorPartWrapper) part).getEditor();
        viewer = xtesa.getTextViewer();
        elDetector.setContext(new TestContext(xtesa));
      } else fail("unsupported editor type - " + ((EditorPartWrapper) part).getEditor().getClass());
    } else if (part instanceof JSPMultiPageEditor) {
      viewer = ((JSPMultiPageEditor) part).getJspEditor().getTextViewer();
      elDetector.setContext(new TestContext(((JSPMultiPageEditor) part).getJspEditor()));
    } else fail("unsupported editor type - " + part.getClass());

    int counter = 0;
    for (int i = 0; i < document.getLength(); i++) {
      int lineNumber = document.getLineOfOffset(i);
      int position = i - document.getLineOffset(lineNumber) + 1;
      lineNumber++;

      TestData testData = new TestData(document, i);
      IHyperlink[] links = elDetector.detectHyperlinks(viewer, testData.getHyperlinkRegion(), true);

      boolean recognized = links != null;

      if (recognized) {
        counter++;
        TestRegion testRegion = findOffsetInRegions(i, regionList);
        if (testRegion == null) {
          String information = findRegionInformation(document, i, regionList);
          fail(
              "Wrong detection for offset - "
                  + i
                  + " (line - "
                  + lineNumber
                  + " position - "
                  + position
                  + ") "
                  + information);
        } else {
          checkTestRegion(links, testRegion);
        }
      } else {
        for (TestRegion testRegion : regionList) {
          if (i >= testRegion.region.getOffset()
              && i <= testRegion.region.getOffset() + testRegion.region.getLength()) {
            fail(
                "Wrong detection for region - "
                    + getRegionInformation(document, testRegion)
                    + " offset - "
                    + i
                    + " (line - "
                    + lineNumber
                    + " position - "
                    + position
                    + ")");
          }
        }
      }
    }

    assertEquals("Wrong recognized region count: ", expected, counter);

    documentProvider.disconnect(editorInput);
  }
示例#6
0
 private void updateAnnotations(IWorkbenchWindow window, TCFNode node, Set<TCFAnnotation> set) {
   if (disposed) return;
   assert Thread.currentThread() == display.getThread();
   WorkbenchWindowInfo win_info = windows.get(window);
   if (win_info == null) return;
   Map<IEditorInput, IEditorPart> editors = new HashMap<IEditorInput, IEditorPart>();
   Map<IViewPart, ITCFDisassemblyPart> views = new HashMap<IViewPart, ITCFDisassemblyPart>();
   IWorkbenchPage page = window.getActivePage();
   if (page != null) {
     for (IEditorReference ref : page.getEditorReferences()) {
       IEditorPart editor = ref.getEditor(false);
       if (editor == null) continue;
       editors.put(editor.getEditorInput(), editor);
     }
     for (IViewReference ref : page.getViewReferences()) {
       IViewPart view = ref.getView(false);
       if (view == null) continue;
       ITCFDisassemblyPart disasm =
           (ITCFDisassemblyPart) view.getAdapter(ITCFDisassemblyPart.class);
       if (disasm != null) views.put(view, disasm);
     }
   }
   boolean flush_all =
       node == null
           || !views.keySet().equals(win_info.views.keySet())
           || !editors.equals(win_info.editors)
           || changed_launch_cfgs.contains(node.launch);
   win_info.views.clear();
   win_info.views.putAll(views);
   win_info.editors.clear();
   win_info.editors.putAll(editors);
   Iterator<TCFAnnotation> i = win_info.annotations.iterator();
   while (i.hasNext()) {
     TCFAnnotation a = i.next();
     if (!flush_all && set != null && set.remove(a)) continue;
     a.dispose();
     i.remove();
   }
   if (set != null) win_info.annotations.addAll(set);
   ISourcePresentation presentation = TCFModelPresentation.getDefault();
   // Disassembly views
   for (TCFAnnotation a : win_info.annotations) {
     if (a.addr == null) continue;
     for (ITCFDisassemblyPart disasm : views.values()) {
       IAnnotationModel ann_model = disasm.getAnnotationModel();
       if (ann_model == null) continue;
       if (a.models.contains(ann_model)) {
         ann_model.removeAnnotation(a);
         a.models.remove(ann_model);
       }
       Position p = disasm.getAddressPosition(a.addr);
       if (p == null) continue;
       if (a.breakpoint != null && hidePlantingAnnotation(ann_model, a.breakpoint, p)) continue;
       ann_model.addAnnotation(a, p);
       a.models.add(ann_model);
     }
   }
   // Disassembly editor
   for (TCFAnnotation a : win_info.annotations) {
     if (a.addr == null) continue;
     IEditorPart editor = editors.get(TCFModel.DisassemblyEditorInput.INSTANCE);
     if (editor == null) continue;
     ITCFDisassemblyPart disasm =
         (ITCFDisassemblyPart) editor.getAdapter(ITCFDisassemblyPart.class);
     if (disasm == null) continue;
     IAnnotationModel ann_model = disasm.getAnnotationModel();
     if (ann_model == null) continue;
     if (a.models.contains(ann_model)) {
       ann_model.removeAnnotation(a);
       a.models.remove(ann_model);
     }
     Position p = disasm.getAddressPosition(a.addr);
     if (p == null) continue;
     if (a.breakpoint != null && hidePlantingAnnotation(ann_model, a.breakpoint, p)) continue;
     ann_model.addAnnotation(a, p);
     a.models.add(ann_model);
   }
   // Source editors
   if (set == null) return;
   for (TCFAnnotation a : set) {
     if (a.area == null) continue;
     Object source_element = TCFSourceLookupDirector.lookup(node.launch, a.ctx, a.area);
     if (source_element == null) continue;
     IEditorInput editor_input = presentation.getEditorInput(source_element);
     IEditorPart editor = editors.get(editor_input);
     if (!(editor instanceof ITextEditor)) continue;
     IDocumentProvider doc_provider = ((ITextEditor) editor).getDocumentProvider();
     IAnnotationModel ann_model = doc_provider.getAnnotationModel(editor_input);
     if (ann_model == null) continue;
     IRegion region = null;
     try {
       doc_provider.connect(editor_input);
     } catch (CoreException e) {
     }
     try {
       IDocument document = doc_provider.getDocument(editor_input);
       if (document != null) region = document.getLineInformation(a.area.start_line - 1);
     } catch (BadLocationException e) {
     } finally {
       doc_provider.disconnect(editor_input);
     }
     if (region == null) continue;
     Position p = new Position(region.getOffset(), region.getLength());
     if (a.breakpoint != null && hidePlantingAnnotation(ann_model, a.breakpoint, p)) continue;
     ann_model.addAnnotation(a, p);
     a.models.add(ann_model);
   }
 }