1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Fix for 9551: [Open Declaration] does not highlight proper offsets with CTags when Indexer is required

This commit is contained in:
Bogdan Gheorghe 2005-05-17 17:11:12 +00:00
parent 47c79f6013
commit 55f4eca137
5 changed files with 72 additions and 111 deletions

View file

@ -145,8 +145,10 @@ public class CSearchResultPage extends AbstractTextSearchViewPage {
return; return;
try { try {
startOffset = doc.getLineOffset(tempstartLine); // NOTE: Subtract 1 from the passed in line number because, even though the editor is 1 based, the line
length=doc.getLineLength(tempstartLine); //resolver doesn't take this into account and is still 0 based
startOffset = doc.getLineOffset(tempstartLine-1);
length=doc.getLineLength(tempstartLine-1);
} catch (BadLocationException e) {} } catch (BadLocationException e) {}
//See if an end line number has been provided - if so //See if an end line number has been provided - if so
@ -156,7 +158,8 @@ public class CSearchResultPage extends AbstractTextSearchViewPage {
if (tempendLine>0 && tempendLine > tempstartLine){ if (tempendLine>0 && tempendLine > tempstartLine){
int endOffset; int endOffset;
try { try {
endOffset = doc.getLineOffset(tempendLine); //See NOTE above
endOffset = doc.getLineOffset(tempendLine-1);
length = endOffset - startOffset; length = endOffset - startOffset;
} catch (BadLocationException e) {} } catch (BadLocationException e) {}

View file

@ -27,10 +27,8 @@ import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.search.DOMSearchUtil; import org.eclipse.cdt.core.search.DOMSearchUtil;
import org.eclipse.cdt.core.search.ICSearchConstants; import org.eclipse.cdt.core.search.ICSearchConstants;
import org.eclipse.cdt.core.search.ILineLocatable;
import org.eclipse.cdt.core.search.IMatch; import org.eclipse.cdt.core.search.IMatch;
import org.eclipse.cdt.core.search.IMatchLocatable; import org.eclipse.cdt.core.search.OffsetLocatable;
import org.eclipse.cdt.core.search.IOffsetLocatable;
import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.model.CProject; import org.eclipse.cdt.internal.core.model.CProject;
import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditor;
@ -43,8 +41,6 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog; import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.texteditor.IUpdate; import org.eclipse.ui.texteditor.IUpdate;
public class OpenDeclarationsAction extends SelectionParseAction implements IUpdate { public class OpenDeclarationsAction extends SelectionParseAction implements IUpdate {
@ -161,8 +157,7 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
if (fileName != null) { if (fileName != null) {
storage.setFileName(fileName); storage.setFileName(fileName);
storage.setLength(end - start); storage.setLocatable(new OffsetLocatable(start,end));
storage.setOffset(start);
storage.setResource(ParserUtil.getResourceForFilename( fileName )); storage.setResource(ParserUtil.getResourceForFilename( fileName ));
} else { } else {
operationNotAvailable(CSEARCH_OPERATION_NO_DECLARATION_MESSAGE); operationNotAvailable(CSEARCH_OPERATION_NO_DECLARATION_MESSAGE);
@ -180,34 +175,7 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
if (match instanceof IMatch) { if (match instanceof IMatch) {
IMatch theMatch = (IMatch)match; IMatch theMatch = (IMatch)match;
storage.setFileName(theMatch.getLocation().toOSString()); storage.setFileName(theMatch.getLocation().toOSString());
IMatchLocatable searchLocatable = theMatch.getLocatable(); storage.setLocatable(theMatch.getLocatable());
int startOffset=0;
int length=0;
if (searchLocatable instanceof IOffsetLocatable){
startOffset = ((IOffsetLocatable)searchLocatable).getNameStartOffset();
length = ((IOffsetLocatable)searchLocatable).getNameEndOffset() - startOffset;
} else if (searchLocatable instanceof ILineLocatable){
int tempstartOffset = ((ILineLocatable)searchLocatable).getStartLine();
IDocument doc =fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
try {
startOffset = doc.getLineOffset(tempstartOffset);
length=doc.getLineLength(tempstartOffset);
} catch (BadLocationException e) {}
//Check to see if an end offset is provided
int tempendOffset = ((ILineLocatable)searchLocatable).getEndLine();
//Make sure that there is a real value for the end line
if (tempendOffset>0 && tempendOffset>tempstartOffset){
try {
int endOffset = doc.getLineOffset(tempendOffset);
length=endOffset - startOffset;
} catch (BadLocationException e) {}
}
}
storage.setLength(length);
storage.setOffset(startOffset);
storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString())); storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
break; break;
} }
@ -243,19 +211,17 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell()); ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell());
progressMonitor.run(true, true, runnable); progressMonitor.run(true, true, runnable);
int nameOffset = storage.getOffset();
int nameLength = storage.getLength();
if( storage.getResource() != null ) if( storage.getResource() != null )
{ {
clearStatusLine(); clearStatusLine();
open( storage.getResource(), nameOffset, nameLength ); open( storage.getResource(), storage.getLocatable() );
return; return;
} }
String fileName = storage.getFileName(); String fileName = storage.getFileName();
if (fileName != null){ if (fileName != null){
clearStatusLine(); clearStatusLine();
open( fileName, nameOffset, nameLength); open( fileName, storage.getLocatable());
} }
} catch(Exception x) { } catch(Exception x) {

View file

@ -26,10 +26,8 @@ import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.util.ArrayUtil; import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.search.DOMSearchUtil; import org.eclipse.cdt.core.search.DOMSearchUtil;
import org.eclipse.cdt.core.search.ICSearchConstants; import org.eclipse.cdt.core.search.ICSearchConstants;
import org.eclipse.cdt.core.search.ILineLocatable;
import org.eclipse.cdt.core.search.IMatch; import org.eclipse.cdt.core.search.IMatch;
import org.eclipse.cdt.core.search.IMatchLocatable; import org.eclipse.cdt.core.search.OffsetLocatable;
import org.eclipse.cdt.core.search.IOffsetLocatable;
import org.eclipse.cdt.core.search.SearchEngine; import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.model.CProject; import org.eclipse.cdt.internal.core.model.CProject;
import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditor;
@ -42,8 +40,6 @@ import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog; import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress; import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.texteditor.IUpdate; import org.eclipse.ui.texteditor.IUpdate;
/** /**
@ -173,8 +169,7 @@ public class OpenDefinitionAction extends SelectionParseAction implements
if (fileName != null) { if (fileName != null) {
storage.setFileName(fileName); storage.setFileName(fileName);
storage.setLength(end - start); storage.setLocatable(new OffsetLocatable(start,end));
storage.setOffset(start);
storage.setResource(ParserUtil.getResourceForFilename( fileName )); storage.setResource(ParserUtil.getResourceForFilename( fileName ));
} else { } else {
operationNotAvailable(CSEARCH_OPERATION_NO_DEFINITION_MESSAGE); operationNotAvailable(CSEARCH_OPERATION_NO_DEFINITION_MESSAGE);
@ -192,33 +187,7 @@ public class OpenDefinitionAction extends SelectionParseAction implements
if (match instanceof IMatch) { if (match instanceof IMatch) {
IMatch theMatch = (IMatch)match; IMatch theMatch = (IMatch)match;
storage.setFileName(theMatch.getLocation().toOSString()); storage.setFileName(theMatch.getLocation().toOSString());
IMatchLocatable searchLocatable = theMatch.getLocatable(); storage.setLocatable(theMatch.getLocatable());
int startOffset=0;
int length=0;
if (searchLocatable instanceof IOffsetLocatable){
startOffset = ((IOffsetLocatable)searchLocatable).getNameStartOffset();
length = ((IOffsetLocatable)searchLocatable).getNameEndOffset() - startOffset;
} else if (searchLocatable instanceof ILineLocatable){
int tempstartOffset = ((ILineLocatable)searchLocatable).getStartLine();
IDocument doc =fEditor.getDocumentProvider().getDocument(fEditor.getEditorInput());
try {
startOffset = doc.getLineOffset(tempstartOffset);
length=doc.getLineLength(tempstartOffset);
} catch (BadLocationException e) {}
//Check to see if an end offset is provided
int tempendOffset = ((ILineLocatable)searchLocatable).getEndLine();
//Make sure that there is a real value for the end line
if (tempendOffset>0 && tempendOffset>tempstartOffset){
try {
int endOffset = doc.getLineOffset(tempendOffset);
length=endOffset - startOffset;
} catch (BadLocationException e) {}
}
}
storage.setLength(length);
storage.setOffset(startOffset);
storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString())); storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
break; break;
} }
@ -254,19 +223,17 @@ public class OpenDefinitionAction extends SelectionParseAction implements
ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell()); ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell());
progressMonitor.run(true, true, runnable); progressMonitor.run(true, true, runnable);
int nameOffset = storage.getOffset();
int nameLength = storage.getLength();
if( storage.getResource() != null ) if( storage.getResource() != null )
{ {
clearStatusLine(); clearStatusLine();
open( storage.getResource(), nameOffset, nameLength ); open( storage.getResource(), storage.getLocatable() );
return; return;
} }
String fileName = storage.getFileName(); String fileName = storage.getFileName();
if (fileName != null){ if (fileName != null){
clearStatusLine(); clearStatusLine();
open( fileName, nameOffset, nameLength); open( fileName, storage.getLocatable() );
} }
} catch(Exception x) { } catch(Exception x) {
CUIPlugin.getDefault().log(x); CUIPlugin.getDefault().log(x);

View file

@ -32,6 +32,9 @@ import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.ParserUtil; import org.eclipse.cdt.core.parser.ParserUtil;
import org.eclipse.cdt.core.parser.ScannerInfo; import org.eclipse.cdt.core.parser.ScannerInfo;
import org.eclipse.cdt.core.resources.FileStorage; import org.eclipse.cdt.core.resources.FileStorage;
import org.eclipse.cdt.core.search.ILineLocatable;
import org.eclipse.cdt.core.search.IMatchLocatable;
import org.eclipse.cdt.core.search.IOffsetLocatable;
import org.eclipse.cdt.internal.ui.editor.CEditor; import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.search.CSearchMessages; import org.eclipse.cdt.internal.ui.search.CSearchMessages;
import org.eclipse.cdt.internal.ui.util.EditorUtility; import org.eclipse.cdt.internal.ui.util.EditorUtility;
@ -553,8 +556,7 @@ public class SelectionParseAction extends Action {
{ {
private IResource resource; private IResource resource;
private String fileName; private String fileName;
private int offset=0; private IMatchLocatable locatable;
private int length=0;
public final String getFileName() { public final String getFileName() {
return fileName; return fileName;
@ -564,20 +566,14 @@ public class SelectionParseAction extends Action {
public final IResource getResource() { public final IResource getResource() {
return resource; return resource;
} }
public int getLength() { public final IMatchLocatable getLocatable() {
return length; return locatable;
} }
public int getOffset() {
return offset;
}
public void setFileName(String fileName) { public void setFileName(String fileName) {
this.fileName = fileName; this.fileName = fileName;
} }
public void setLength(int length) { public void setLocatable(IMatchLocatable locatable) {
this.length = length; this.locatable = locatable;
}
public void setOffset(int offset) {
this.offset = offset;
} }
public void setResource(IResource resource) { public void setResource(IResource resource) {
this.resource = resource; this.resource = resource;
@ -589,25 +585,25 @@ public class SelectionParseAction extends Action {
* @param string * @param string
* @param i * @param i
*/ */
protected boolean open(String filename, int offset, int length) throws PartInitException, CModelException { protected boolean open(String filename, IMatchLocatable locatable) throws PartInitException, CModelException {
IPath path = new Path( filename ); IPath path = new Path( filename );
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path); IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
if( file != null ) if( file != null )
{ {
open( file, offset, length ); open( file, locatable );
return true; return true;
} }
ICProject cproject = CoreModel.getDefault().getCModel().getCProject( projectName ); ICProject cproject = CoreModel.getDefault().getCModel().getCProject( projectName );
ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, path); ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, path);
if (unit != null) { if (unit != null) {
setSelectionAtOffset( EditorUtility.openInEditor(unit), offset, length ); setSelectionAtOffset( EditorUtility.openInEditor(unit), locatable );
return true; return true;
} }
FileStorage storage = new FileStorage(null, path); FileStorage storage = new FileStorage(null, path);
IEditorPart part = EditorUtility.openInEditor(storage); IEditorPart part = EditorUtility.openInEditor(storage);
setSelectionAtOffset(part, offset, length); setSelectionAtOffset(part, locatable);
return true; return true;
} }
@ -618,9 +614,9 @@ public class SelectionParseAction extends Action {
/** /**
* Opens the editor on the given element and subsequently selects it. * Opens the editor on the given element and subsequently selects it.
*/ */
protected void open( IResource resource, int offset, int length ) throws CModelException, PartInitException { protected void open( IResource resource, IMatchLocatable locatable ) throws CModelException, PartInitException {
IEditorPart part= EditorUtility.openInEditor(resource); IEditorPart part= EditorUtility.openInEditor(resource);
setSelectionAtOffset(part, offset, length); setSelectionAtOffset(part, locatable);
} }
/** /**
@ -628,11 +624,40 @@ public class SelectionParseAction extends Action {
* @param offset * @param offset
* @param length TODO * @param length TODO
*/ */
protected void setSelectionAtOffset(IEditorPart part, int offset, int length) { protected void setSelectionAtOffset(IEditorPart part, IMatchLocatable locatable) {
if( part instanceof AbstractTextEditor ) if( part instanceof AbstractTextEditor )
{ {
int startOffset=0;
int length=0;
if (locatable instanceof IOffsetLocatable){
startOffset = ((IOffsetLocatable)locatable).getNameStartOffset();
length = ((IOffsetLocatable)locatable).getNameEndOffset() - startOffset;
} else if (locatable instanceof ILineLocatable){
int tempstartOffset = ((ILineLocatable)locatable).getStartLine();
IDocument doc = ((AbstractTextEditor) part).getDocumentProvider().getDocument(part.getEditorInput());;
try {
//NOTE: Subtract 1 from the passed in line number because, even though the editor is 1 based, the line
//resolver doesn't take this into account and is still 0 based
startOffset = doc.getLineOffset(tempstartOffset-1);
length=doc.getLineLength(tempstartOffset-1);
} catch (BadLocationException e) {}
//Check to see if an end offset is provided
int tempendOffset = ((ILineLocatable)locatable).getEndLine();
//Make sure that there is a real value for the end line
if (tempendOffset>0 && tempendOffset>tempstartOffset){
try {
//See NOTE above
int endOffset = doc.getLineOffset(tempendOffset-1);
length=endOffset - startOffset;
} catch (BadLocationException e) {}
}
}
try { try {
((AbstractTextEditor) part).selectAndReveal(offset, length); ((AbstractTextEditor) part).selectAndReveal(startOffset, length);
} catch (Exception e) {} } catch (Exception e) {}
} }
} }

View file

@ -205,8 +205,10 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
int tempstartOffset = ((ILineLocatable)searchLocatable).getStartLine(); int tempstartOffset = ((ILineLocatable)searchLocatable).getStartLine();
IDocument doc =textViewer.getDocument(); IDocument doc =textViewer.getDocument();
try { try {
startOffset = doc.getLineOffset(tempstartOffset); //NOTE: Subtract 1 from the passed in line number because, even though the editor is 1 based, the line
length=doc.getLineLength(tempstartOffset); //resolver doesn't take this into account and is still 0 based
startOffset = doc.getLineOffset(tempstartOffset-1);
length=doc.getLineLength(tempstartOffset-1);
} catch (BadLocationException e) {} } catch (BadLocationException e) {}
//Check to see if an end offset is provided //Check to see if an end offset is provided
@ -214,7 +216,8 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
//Make sure that there is a real value for the end line //Make sure that there is a real value for the end line
if (tempendOffset>0 && tempendOffset>tempstartOffset){ if (tempendOffset>0 && tempendOffset>tempstartOffset){
try { try {
int endOffset = doc.getLineOffset(tempendOffset); //See NOTE above
int endOffset = doc.getLineOffset(tempendOffset-1);
length=endOffset - startOffset; length=endOffset - startOffset;
} catch (BadLocationException e) {} } catch (BadLocationException e) {}
} }
@ -225,11 +228,8 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
} }
} }
} }
}catch (InterruptedException e) { }catch (InterruptedException e) {}
// catch (CModelException e) {}
} catch (CModelException e) {
//
}
} }
} }
return null; return null;