您的位置:首页 > 编程语言

CEditor里鼠标悬停在word上显示对应声明代码的提示框的代码

2014-04-15 19:26 459 查看
org.eclipse.jface.text.TextViewerHoverManager



/**
	 * Determines all necessary details and delegates the computation into
	 * a background thread.
	 */
	protected void computeInformation() {

		if (!fProcessMouseHoverEvent) {
			setInformation(null, null);
			return;
		}

		Point location= getHoverEventLocation();
		int offset= computeOffsetAtLocation(location.x, location.y);
		if (offset == -1) {
			setInformation(null, null);
			return;
		}

		final ITextHover hover= fTextViewer.getTextHover(offset, getHoverEventStateMask());
		if (hover == null) {
			setInformation(null, null);
			return;
		}

		final IRegion region= hover.getHoverRegion(fTextViewer, offset);
		if (region == null) {
			setInformation(null, null);
			return;
		}

		final Rectangle area= JFaceTextUtil.computeArea(region, fTextViewer);
		if (area == null || area.isEmpty()) {
			setInformation(null, null);
			return;
		}

		if (fThread != null) {
			setInformation(null, null);
			return;
		}

		fThread= new Thread("Text Viewer Hover Presenter") { //$NON-NLS-1$
			public void run() {
				// http://bugs.eclipse.org/bugs/show_bug.cgi?id=17693 				boolean hasFinished= false;
				try {
					if (fThread != null) {
						Object information;
						try {    //此处获取提示信息
							if (hover instanceof ITextHoverExtension2)
								information= ((ITextHoverExtension2)hover).getHoverInfo2(fTextViewer, region);
							else
								information= hover.getHoverInfo(fTextViewer, region);
						} catch (ArrayIndexOutOfBoundsException x) {
							/*
							 * This code runs in a separate thread which can
							 * lead to text offsets being out of bounds when
							 * computing the hover info (see bug 32848).
							 */
							information= null;
						}

						if (hover instanceof ITextHoverExtension)
							setCustomInformationControlCreator(((ITextHoverExtension) hover).getHoverControlCreator());
						else
							setCustomInformationControlCreator(null);

						setInformation(information, area);//此处设置提示信息
						if (information != null)
							fTextHover= hover;
					} else {
						setInformation(null, null);
					}
					hasFinished= true;
				} catch (RuntimeException ex) {
					String PLUGIN_ID= "org.eclipse.jface.text"; //$NON-NLS-1$
					ILog log= Platform.getLog(Platform.getBundle(PLUGIN_ID));
					log.log(new Status(IStatus.ERROR, PLUGIN_ID, IStatus.OK, "Unexpected runtime error while computing a text hover", ex)); //$NON-NLS-1$
				} finally {
					synchronized (fMutex) {
						if (fTextViewer != null)
							fTextViewer.removeTextListener(fStopper);
						fThread= null;
						// https://bugs.eclipse.org/bugs/show_bug.cgi?id=44756 						if (!hasFinished)
							setInformation(null, null);
					}
				}
			}
		};

		fThread.setDaemon(true);
		fThread.setPriority(Thread.MIN_PRIORITY);
		synchronized (fMutex) {
			fTextViewer.addTextListener(fStopper);
			fThread.start();
		}
	}


org.eclipse.cdt.internal.ui.text.c.hover.CSourceHover

public String getHoverInfo(ITextViewer textViewer, IRegion hoverRegion) {
		IEditorPart editor = getEditor();
		if (editor != null) {
			IEditorInput input= editor.getEditorInput();
			IWorkingCopyManager manager= CUIPlugin.getDefault().getWorkingCopyManager();				
			IWorkingCopy copy = manager.getWorkingCopy(input);
			try {
				if (copy == null || !copy.isConsistent()) {
					return null;
				}
			} catch (CModelException exc) {
				return null;
			}
			
			String expression;
			try {
				expression = textViewer.getDocument().get(hoverRegion.getOffset(), hoverRegion.getLength());
				expression = expression.trim();
				if (expression.length() == 0)
					return null;

				//Before trying a search lets make sure that the user is not hovering over a keyword 
				if (selectionIsKeyword(expression))
					return null;

				String source= null;

				// Try with the indexer 在indexer中查找
				source= searchInIndex(copy, hoverRegion);

				if (source == null || source.trim().length() == 0)
					return null;

				// we are actually interested in the comments, too.
//				source= removeLeadingComments(source);

				String delim= System.getProperty("line.separator", "\n"); //$NON-NLS-1$ //$NON-NLS-2$

				String[] sourceLines= Strings.convertIntoLines(source);
				String firstLine= sourceLines[0];
				boolean ignoreFirstLine = firstLine.length() > 0 && !Character.isWhitespace(firstLine.charAt(0));
				Strings.trimIndentation(sourceLines, getTabWidth(), getTabWidth(), !ignoreFirstLine);

				source = Strings.concatenate(sourceLines, delim);
				return source;

			} catch (BadLocationException e) {
			}
		}
		return null;
	}


相关类:

org.eclipse.jface.text.AbstractInformationControlManager

org.eclipse.cdt.internal.ui.text.c.hover.BestMatchHover

org.eclipse.cdt.internal.ui.text.c.hover.CEditorTextHoverDescriptor

org.eclipse.cdt.internal.ui.text.c.hover.CEditorTextHoverProxy

org.eclipse.cdt.internal.ui.text.c.hover.CSourceHover

org.eclipse.cdt.internal.ui.text.c.hover.CSourceHover.ComputeSourceRunnable

org.eclipse.cdt.internal.ui.text.c.hover.SourceViewerInformationControl

org.eclipse.jface.text.TextViewerHoverManager
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: