Exemple #1
0
  /* (non-Javadoc)
   * @see org.eclipse.jface.text.ITextHover#getHoverInfo(org.eclipse.jface.text.ITextViewer, org.eclipse.jface.text.IRegion)
   */
  public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
    ScriptStackFrame frame = getFrame();
    if (frame == null) {
      return null;
    }
    IDocument document = textViewer.getDocument();
    if (document == null) {
      return null;
    }
    try {
      String str =
          TextUtilities.getContentType(
              document,
              IDocumentExtension3.DEFAULT_PARTITIONING,
              hoverRegion.getOffset() + 1,
              true);

      String variableName = document.get(hoverRegion.getOffset(), hoverRegion.getLength());

      if (JSPartitionScanner.JS_KEYWORD.equals(str) && !"this".equals(variableName)) // $NON-NLS-1$
      {
        return null;
      }
      ScriptValue var = ((ScriptDebugTarget) frame.getDebugTarget()).evaluate(frame, variableName);
      if (var != null) {
        return getVariableText(var);
      }
    } catch (BadLocationException e) {
      return null;
    }
    return null;
  }
  /**
   * Returns the indentation of the line <code>line</code> in <code>document</code>. The returned
   * string may contain pairs of leading slashes that are considered part of the indentation. The
   * space before the asterix in a javadoc-like comment is not considered part of the indentation.
   *
   * @param document the document
   * @param line the line
   * @return the indentation of <code>line</code> in <code>document</code>
   * @throws BadLocationException if the document is changed concurrently
   */
  private static String getCurrentIndent(IDocument document, int line) throws BadLocationException {
    IRegion region = document.getLineInformation(line);
    int from = region.getOffset();
    int endOffset = region.getOffset() + region.getLength();

    // go behind line comments
    int to = from;
    while (to < endOffset - 2 && document.get(to, 2).equals(SLASHES)) to += 2;

    while (to < endOffset) {
      char ch = document.getChar(to);
      if (!Character.isWhitespace(ch)) break;
      to++;
    }

    // don't count the space before javadoc like, asterix-style comment lines
    if (to > from && to < endOffset - 1 && document.get(to - 1, 2).equals(" *")) { // $NON-NLS-1$
      String type =
          TextUtilities.getContentType(document, IJavaPartitions.JAVA_PARTITIONING, to, true);
      if (type.equals(IJavaPartitions.JAVA_DOC)
          || type.equals(IJavaPartitions.JAVA_MULTI_LINE_COMMENT)) to--;
    }

    return document.get(from, to - from);
  }
 /**
  * Returns true if the character at the specified offset is a less-than sign, rather than an type
  * parameter list open angle bracket.
  *
  * @param document a document
  * @param offset an offset within the document
  * @return true if the character at the specified offset is not a type parameter start bracket
  * @throws BadLocationException
  */
 private boolean isLessThanOperator(IDocument document, int offset) throws BadLocationException {
   if (offset < 0) return false;
   JavaHeuristicScanner scanner =
       new JavaHeuristicScanner(
           document,
           IJavaPartitions.JAVA_PARTITIONING,
           TextUtilities.getContentType(
               document, IJavaPartitions.JAVA_PARTITIONING, offset, false));
   return !isTypeParameterBracket(offset, document, scanner);
 }
  private static boolean isInJavadoc(JavaEditor editor) {
    ITextSelection selection = getTextSelection(editor);
    if (selection == null) return false;

    IDocument document = editor.getDocumentProvider().getDocument(editor.getEditorInput());
    try {
      String contentType =
          TextUtilities.getContentType(
              document, IJavaPartitions.JAVA_PARTITIONING, selection.getOffset(), true);
      return contentType.equals(IJavaPartitions.JAVA_DOC);
    } catch (BadLocationException e) {
      return false;
    }
  }
    /**
     * Tries to make a text hover focusable (or "sticky").
     *
     * @param sourceViewer the source viewer to display the hover over
     * @param textHover the hover to make focusable
     * @return <code>true</code> if successful, <code>false</code> otherwise
     */
    @SuppressWarnings("deprecation")
    private boolean makeTextHoverFocusable(ISourceViewer sourceViewer, ITextHover textHover) {
      Point hoverEventLocation = ((ITextViewerExtension2) sourceViewer).getHoverEventLocation();
      int offset =
          computeOffsetAtLocation(sourceViewer, hoverEventLocation.x, hoverEventLocation.y);
      if (offset == -1) return false;

      try {
        IRegion hoverRegion = textHover.getHoverRegion(sourceViewer, offset);
        if (hoverRegion == null) return false;

        String hoverInfo = textHover.getHoverInfo(sourceViewer, hoverRegion);

        IInformationControlCreator controlCreator = null;
        if (textHover instanceof IInformationProviderExtension2)
          controlCreator =
              ((IInformationProviderExtension2) textHover).getInformationPresenterControlCreator();

        IInformationProvider informationProvider =
            new InformationProvider(hoverRegion, hoverInfo, controlCreator);

        fInformationPresenter.setOffset(offset);
        fInformationPresenter.setAnchor(AbstractInformationControlManager.ANCHOR_BOTTOM);
        fInformationPresenter.setMargins(
            6, 6); // default values from AbstractInformationControlManager
        String contentType =
            TextUtilities.getContentType(
                sourceViewer.getDocument(), AutoconfPartitionScanner.AUTOCONF_MACRO, offset, true);
        fInformationPresenter.setInformationProvider(informationProvider, contentType);
        fInformationPresenter.showInformation();

        return true;

      } catch (BadLocationException e) {
        return false;
      }
    }
 /**
  * Returns the partition type located at offset in the document
  *
  * @param document - assumes document is not null
  * @param offset
  * @return String partition type
  */
 protected String getPartitionType(IDocument document, int offset) {
   String type = null;
   try {
     // TODO: provide partitioning information so we're not using a default like this
     if (document instanceof IStructuredDocument) {
       type =
           TextUtilities.getContentType(
               document, IStructuredPartitioning.DEFAULT_STRUCTURED_PARTITIONING, offset, false);
     }
   } catch (BadLocationException e1) {
   } finally {
     if (type == null) {
       try {
         ITypedRegion region = document.getPartition(offset);
         if (region != null) {
           type = region.getType();
         }
       } catch (BadLocationException e) {
         type = null;
       }
     }
   }
   return type;
 }
    /**
     * Tries to make an annotation hover focusable (or "sticky").
     *
     * @param sourceViewer the source viewer to display the hover over
     * @param annotationHover the hover to make focusable
     * @return <code>true</code> if successful, <code>false</code> otherwise
     */
    private boolean makeAnnotationHoverFocusable(
        ISourceViewer sourceViewer, IAnnotationHover annotationHover) {
      IVerticalRulerInfo info = getVerticalRuler();
      int line = info.getLineOfLastMouseButtonActivity();
      if (line == -1) return false;

      try {

        // compute the hover information
        Object hoverInfo;
        if (annotationHover instanceof IAnnotationHoverExtension) {
          IAnnotationHoverExtension extension = (IAnnotationHoverExtension) annotationHover;
          ILineRange hoverLineRange = extension.getHoverLineRange(sourceViewer, line);
          if (hoverLineRange == null) return false;
          final int maxVisibleLines =
              Integer
                  .MAX_VALUE; // allow any number of lines being displayed, as we support scrolling
          hoverInfo = extension.getHoverInfo(sourceViewer, hoverLineRange, maxVisibleLines);
        } else {
          hoverInfo = annotationHover.getHoverInfo(sourceViewer, line);
        }

        // hover region: the beginning of the concerned line to place the control right over the
        // line
        IDocument document = sourceViewer.getDocument();
        int offset = document.getLineOffset(line);
        String contentType =
            TextUtilities.getContentType(
                document, AutoconfPartitionScanner.AUTOCONF_MACRO, offset, true);

        IInformationControlCreator controlCreator = null;

        //    			/*
        //    			 * XXX: This is a hack to avoid API changes at the end of 3.2,
        //    			 * and should be fixed for 3.3, see:
        // https://bugs.eclipse.org/bugs/show_bug.cgi?id=137967
        //    			 */
        //    			if
        // ("org.eclipse.jface.text.source.projection.ProjectionAnnotationHover".equals(annotationHover.getClass().getName())) { //$NON-NLS-1$
        //    				controlCreator= new IInformationControlCreator() {
        //    					public IInformationControl createInformationControl(Shell shell) {
        //    						int shellStyle= SWT.RESIZE | SWT.TOOL | getOrientation();
        //    						int style= SWT.V_SCROLL | SWT.H_SCROLL;
        //    						return new SourceViewerInformationControl(shell, shellStyle, style);
        //    					}
        //    				};
        //
        //    			} else {
        if (annotationHover instanceof IInformationProviderExtension2)
          controlCreator =
              ((IInformationProviderExtension2) annotationHover)
                  .getInformationPresenterControlCreator();
        else if (annotationHover instanceof IAnnotationHoverExtension)
          controlCreator = ((IAnnotationHoverExtension) annotationHover).getHoverControlCreator();
        //    			}

        IInformationProvider informationProvider =
            new InformationProvider(new Region(offset, 0), hoverInfo, controlCreator);

        fInformationPresenter.setOffset(offset);
        fInformationPresenter.setAnchor(AbstractInformationControlManager.ANCHOR_RIGHT);
        fInformationPresenter.setMargins(
            4, 0); // AnnotationBarHoverManager sets (5,0), minus SourceViewer.GAP_SIZE_1
        fInformationPresenter.setInformationProvider(informationProvider, contentType);
        fInformationPresenter.showInformation();

        return true;

      } catch (BadLocationException e) {
        return false;
      }
    }
  protected void assertContentAssistResults(
      int offset,
      int length,
      String[] expected,
      boolean isCompletion,
      boolean isTemplate,
      boolean filterResults,
      int compareType)
      throws Exception {
    if (CTestPlugin.getDefault().isDebugging()) {
      System.out.println("\n\n\n\n\nTesting " + this.getClass().getName());
    }

    // Call the CContentAssistProcessor
    ISourceViewer sourceViewer = EditorTestHelper.getSourceViewer((AbstractTextEditor) fEditor);
    String contentType =
        TextUtilities.getContentType(
            sourceViewer.getDocument(), ICPartitions.C_PARTITIONING, offset, true);
    boolean isCode = IDocument.DEFAULT_CONTENT_TYPE.equals(contentType);
    ContentAssistant assistant = new ContentAssistant();
    CContentAssistProcessor processor =
        new CContentAssistProcessor(fEditor, assistant, contentType);
    long startTime = System.currentTimeMillis();
    sourceViewer.setSelectedRange(offset, length);
    Object[] results =
        isCompletion
            ? (Object[]) processor.computeCompletionProposals(sourceViewer, offset)
            : (Object[]) processor.computeContextInformation(sourceViewer, offset);
    long endTime = System.currentTimeMillis();
    assertTrue(results != null);

    if (filterResults) {
      if (isTemplate) {
        results = filterResultsKeepTemplates(results);
      } else {
        results = filterResults(results, isCode);
      }
    }
    String[] resultStrings = toStringArray(results, compareType);
    Arrays.sort(expected);
    Arrays.sort(resultStrings);

    if (CTestPlugin.getDefault().isDebugging()) {
      System.out.println("Time (ms): " + (endTime - startTime));
      for (String proposal : resultStrings) {
        System.out.println("Result: " + proposal);
      }
    }

    boolean allFound = true; // for the time being, let's be optimistic

    for (String element : expected) {
      boolean found = false;
      for (String proposal : resultStrings) {
        if (element.equals(proposal)) {
          found = true;
          if (CTestPlugin.getDefault().isDebugging()) {
            System.out.println("Lookup success for " + element);
          }
          break;
        }
      }
      if (!found) {
        allFound = false;
        if (CTestPlugin.getDefault().isDebugging()) {
          System.out.println("Lookup failed for " + element); // $NON-NLS-1$
        }
      }
    }

    if (!allFound) {
      assertEquals("Missing results!", toString(expected), toString(resultStrings));
    } else if (doCheckExtraResults()) {
      assertEquals("Extra results!", toString(expected), toString(resultStrings));
    }
  }