1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-26 18:35:32 +02:00

Support for text hovering.

This commit is contained in:
Alain Magloire 2004-02-28 16:16:12 +00:00
parent cd8f8ca00f
commit f977490590
5 changed files with 209 additions and 36 deletions

View file

@ -12,12 +12,15 @@ package org.eclipse.cdt.make.internal.ui.editor;
import org.eclipse.cdt.make.internal.ui.text.IMakefileColorManager;
import org.eclipse.cdt.make.internal.ui.text.MakefileColorManager;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefileAnnotationHover;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefileCodeScanner;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefileCompletionProcessor;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefilePartitionScanner;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefileReconcilingStrategy;
import org.eclipse.cdt.make.internal.ui.text.makefile.MakefileTextHover;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.contentassist.ContentAssistant;
import org.eclipse.jface.text.contentassist.IContentAssistant;
@ -28,6 +31,7 @@ import org.eclipse.jface.text.reconciler.MonoReconciler;
import org.eclipse.jface.text.rules.BufferedRuleBasedScanner;
import org.eclipse.jface.text.rules.DefaultDamagerRepairer;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.source.IAnnotationHover;
import org.eclipse.jface.text.source.ISourceViewer;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
import org.eclipse.swt.graphics.RGB;
@ -162,4 +166,16 @@ public class MakefileSourceConfiguration extends SourceViewerConfiguration {
return new String[]{"#"};
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.source.SourceViewerConfiguration#getTextHover(org.eclipse.jface.text.source.ISourceViewer, java.lang.String)
*/
public ITextHover getTextHover(ISourceViewer sourceViewer, String contentType) {
return new MakefileTextHover(fEditor);
}
/* (non-Javadoc)
* @see org.eclipse.jface.text.source.SourceViewerConfiguration#getAnnotationHover(org.eclipse.jface.text.source.ISourceViewer)
*/
public IAnnotationHover getAnnotationHover(ISourceViewer sourceViewer) {
return new MakefileAnnotationHover();
}
}

View file

@ -11,6 +11,7 @@ package org.eclipse.cdt.make.internal.ui.text;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextViewer;
/**
@ -19,7 +20,7 @@ import org.eclipse.jface.text.ITextViewer;
public class WordPartDetector {
String wordPart = ""; //$NON-NLS-1$
int offset;
/**
* Method WordPartDetector.
* @param viewer is a text viewer
@ -30,8 +31,11 @@ public class WordPartDetector {
int endOffset = documentOffset;
try {
IDocument doc = viewer.getDocument();
int bottom = viewer.getBottomIndexEndOffset();
int top = viewer.getTopIndexStartOffset();
//int bottom = viewer.getBottomIndexEndOffset();
//int top = viewer.getTopIndexStartOffset();
IRegion region = doc.getLineInformationOfOffset(documentOffset);
int top = region.getOffset();
int bottom = region.getLength() + top;
while (offset >= top && isMakefileLetter(doc.getChar(offset))) {
offset--;
}
@ -45,12 +49,33 @@ public class WordPartDetector {
// do nothing
}
}
public static boolean inMacro(ITextViewer viewer, int offset) {
boolean isMacro = false;
IDocument document = viewer.getDocument();
// Try to figure out if we are in a Macro.
try {
for (int index = offset - 1; index >= 0; index--) {
char c;
c = document.getChar(index);
if (c == '$') {
isMacro = true;
break;
} else if (Character.isWhitespace(c)) {
break;
}
}
} catch (BadLocationException e) {
}
return isMacro;
}
/**
* Method getString.
* @return String
*/
public String getString() {
public String toString() {
return wordPart;
}

View file

@ -0,0 +1,52 @@
/**********************************************************************
* Copyright (c) 2002,2003 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.ui.text.makefile;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.source.IAnnotationHover;
import org.eclipse.jface.text.source.ISourceViewer;
/**
* MakefileAnnotationHover
*
*/
public class MakefileAnnotationHover implements IAnnotationHover {
/**
*
*/
public MakefileAnnotationHover() {
super();
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.source.IAnnotationHover#getHoverInfo(org.eclipse.jface.text.source.ISourceViewer,
* int)
*/
public String getHoverInfo(ISourceViewer sourceViewer, int lineNumber) {
IDocument document = sourceViewer.getDocument();
try {
IRegion info = document.getLineInformation(lineNumber);
return document.get(info.getOffset(), info.getLength());
} catch (BadLocationException x) {
}
return null;
}
}

View file

@ -14,17 +14,15 @@ import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import org.eclipse.cdt.make.core.makefile.IDirective;
import org.eclipse.cdt.make.core.makefile.IMacroDefinition;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.core.makefile.IRule;
import org.eclipse.cdt.make.core.makefile.IDirective;
import org.eclipse.cdt.make.internal.ui.MakeUIImages;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.cdt.make.internal.ui.text.CompletionProposalComparator;
import org.eclipse.cdt.make.internal.ui.text.WordPartDetector;
import org.eclipse.cdt.make.ui.IWorkingCopyManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.TextPresentation;
import org.eclipse.jface.text.contentassist.CompletionProposal;
@ -119,7 +117,8 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
* @see org.eclipse.jface.text.contentassist.IContentAssistProcessor#computeCompletionProposals(org.eclipse.jface.text.ITextViewer, int)
*/
public ICompletionProposal[] computeCompletionProposals(ITextViewer viewer, int documentOffset) {
boolean macro = inMacro(viewer, documentOffset);
WordPartDetector wordPart = new WordPartDetector(viewer, documentOffset);
boolean macro = WordPartDetector.inMacro(viewer, documentOffset);
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
IDirective[] statements = null;
if (macro) {
@ -133,7 +132,6 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
}
ArrayList proposalList = new ArrayList(statements.length);
WordPartDetector wordPart = new WordPartDetector(viewer, documentOffset);
// iterate over all the different categories
for (int i = 0; i < statements.length; i++) {
@ -149,14 +147,14 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
image = imageTarget;
infoString = name;
}
if (name != null && name.startsWith(wordPart.getString())) {
if (name != null && name.startsWith(wordPart.toString())) {
IContextInformation info = new ContextInformation(name, infoString);
String displayString = (name.equals(infoString) ? name : name + " - " + infoString);
ICompletionProposal result =
new CompletionProposal(
name,
wordPart.getOffset(),
wordPart.getString().length(),
wordPart.toString().length(),
name.length(),
image,
displayString,
@ -175,7 +173,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
*/
public IContextInformation[] computeContextInformation(ITextViewer viewer, int documentOffset) {
WordPartDetector wordPart = new WordPartDetector(viewer, documentOffset);
boolean macro = inMacro(viewer, documentOffset);
boolean macro = WordPartDetector.inMacro(viewer, documentOffset);
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
ArrayList contextList = new ArrayList();
if (macro) {
@ -183,7 +181,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
for (int i = 0; i < statements.length; i++) {
if (statements[i] instanceof IMacroDefinition) {
String name = ((IMacroDefinition) statements[i]).getName();
if (name != null && name.equals(wordPart.getString())) {
if (name != null && name.equals(wordPart.toString())) {
String value = ((IMacroDefinition) statements[i]).getValue().toString();
if (value != null && value.length() > 0) {
contextList.add(value);
@ -195,7 +193,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
for (int i = 0; i < statements.length; i++) {
if (statements[i] instanceof IMacroDefinition) {
String name = ((IMacroDefinition) statements[i]).getName();
if (name != null && name.equals(wordPart.getString())) {
if (name != null && name.equals(wordPart.toString())) {
String value = ((IMacroDefinition) statements[i]).getValue().toString();
if (value != null && value.length() > 0) {
contextList.add(value);
@ -208,7 +206,7 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
IContextInformation[] result = new IContextInformation[contextList.size()];
for (int i = 0; i < result.length; i++) {
String context = (String)contextList.get(i);
result[i] = new ContextInformation(imageMacro, wordPart.getString(), context);
result[i] = new ContextInformation(imageMacro, wordPart.toString(), context);
}
return result;
@ -242,24 +240,4 @@ public class MakefileCompletionProcessor implements IContentAssistProcessor {
return fValidator;
}
private boolean inMacro(ITextViewer viewer, int offset) {
boolean isMacro = false;
IDocument document = viewer.getDocument();
// Try to figure out if we are in a Macro.
try {
for (int index = offset - 1; index >= 0; index--) {
char c;
c = document.getChar(index);
if (c == '$') {
isMacro = true;
break;
} else if (Character.isWhitespace(c)) {
break;
}
}
} catch (BadLocationException e) {
}
return isMacro;
}
}

View file

@ -0,0 +1,102 @@
/**********************************************************************
* Copyright (c) 2002,2003 QNX Software Systems and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Common Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/cpl-v10.html
*
* Contributors:
* QNX Software Systems - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.make.internal.ui.text.makefile;
import org.eclipse.cdt.make.core.makefile.IMacroDefinition;
import org.eclipse.cdt.make.core.makefile.IMakefile;
import org.eclipse.cdt.make.internal.ui.MakeUIPlugin;
import org.eclipse.cdt.make.internal.ui.text.WordPartDetector;
import org.eclipse.cdt.make.ui.IWorkingCopyManager;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IRegion;
import org.eclipse.jface.text.ITextHover;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.Region;
import org.eclipse.swt.graphics.Point;
import org.eclipse.ui.IEditorPart;
/**
* MakefileTextHover
*
*/
public class MakefileTextHover implements ITextHover {
private IEditorPart fEditor;
/**
*
*/
public MakefileTextHover(IEditorPart editor) {
fEditor = editor;
}
/*
* (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) {
if (hoverRegion != null) {
try {
int len = hoverRegion.getLength();
int offset = hoverRegion.getOffset();
String word = textViewer.getDocument().get(offset, len);
if (fEditor != null && len > -1) {
IWorkingCopyManager fManager = MakeUIPlugin.getDefault().getWorkingCopyManager();
IMakefile makefile = fManager.getWorkingCopy(fEditor.getEditorInput());
IMacroDefinition[] statements;
if (WordPartDetector.inMacro(textViewer, offset)) {
IMacroDefinition[] m1 = makefile.getMacroDefinitions();
IMacroDefinition[] m2 = makefile.getBuiltinMacroDefinitions();
statements = new IMacroDefinition[m1.length + m2.length];
System.arraycopy(m1, 0, statements, 0, m1.length);
System.arraycopy(m2, 0, statements, m1.length, m2.length);
} else {
statements = new IMacroDefinition[0];
}
// iterate over all the different categories
WordPartDetector wordPart = new WordPartDetector(textViewer, offset);
StringBuffer buffer = new StringBuffer();
for (int i = 0; i < statements.length; i++) {
String name = statements[i].getName();
String infoString = statements[i].getValue().toString();
if (name != null && name.equals(wordPart.toString())) {
buffer.append(name);
buffer.append(" - "); //$NON-NLS-1$
buffer.append(infoString);
break;
}
}
return buffer.toString();
}
} catch (BadLocationException e) {
}
}
return ""; //$NON-NLS-1$
}
/*
* (non-Javadoc)
*
* @see org.eclipse.jface.text.ITextHover#getHoverRegion(org.eclipse.jface.text.ITextViewer,
* int)
*/
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
Point selection = textViewer.getSelectedRange();
if (selection.x <= offset && offset < selection.x + selection.y) {
return new Region(selection.x, selection.y);
}
return new Region(offset, 0);
}
}