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;
try {
startOffset = doc.getLineOffset(tempstartLine);
length=doc.getLineLength(tempstartLine);
// 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(tempstartLine-1);
length=doc.getLineLength(tempstartLine-1);
} catch (BadLocationException e) {}
//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){
int endOffset;
try {
endOffset = doc.getLineOffset(tempendLine);
//See NOTE above
endOffset = doc.getLineOffset(tempendLine-1);
length = endOffset - startOffset;
} 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.search.DOMSearchUtil;
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.IMatchLocatable;
import org.eclipse.cdt.core.search.IOffsetLocatable;
import org.eclipse.cdt.core.search.OffsetLocatable;
import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.model.CProject;
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.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.texteditor.IUpdate;
public class OpenDeclarationsAction extends SelectionParseAction implements IUpdate {
@ -161,8 +157,7 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
if (fileName != null) {
storage.setFileName(fileName);
storage.setLength(end - start);
storage.setOffset(start);
storage.setLocatable(new OffsetLocatable(start,end));
storage.setResource(ParserUtil.getResourceForFilename( fileName ));
} else {
operationNotAvailable(CSEARCH_OPERATION_NO_DECLARATION_MESSAGE);
@ -180,34 +175,7 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
if (match instanceof IMatch) {
IMatch theMatch = (IMatch)match;
storage.setFileName(theMatch.getLocation().toOSString());
IMatchLocatable searchLocatable = 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.setLocatable(theMatch.getLocatable());
storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
break;
}
@ -243,19 +211,17 @@ public class OpenDeclarationsAction extends SelectionParseAction implements IUpd
ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell());
progressMonitor.run(true, true, runnable);
int nameOffset = storage.getOffset();
int nameLength = storage.getLength();
if( storage.getResource() != null )
{
clearStatusLine();
open( storage.getResource(), nameOffset, nameLength );
open( storage.getResource(), storage.getLocatable() );
return;
}
String fileName = storage.getFileName();
if (fileName != null){
clearStatusLine();
open( fileName, nameOffset, nameLength);
open( fileName, storage.getLocatable());
}
} 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.search.DOMSearchUtil;
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.IMatchLocatable;
import org.eclipse.cdt.core.search.IOffsetLocatable;
import org.eclipse.cdt.core.search.OffsetLocatable;
import org.eclipse.cdt.core.search.SearchEngine;
import org.eclipse.cdt.internal.core.model.CProject;
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.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.ui.texteditor.IUpdate;
/**
@ -173,8 +169,7 @@ public class OpenDefinitionAction extends SelectionParseAction implements
if (fileName != null) {
storage.setFileName(fileName);
storage.setLength(end - start);
storage.setOffset(start);
storage.setLocatable(new OffsetLocatable(start,end));
storage.setResource(ParserUtil.getResourceForFilename( fileName ));
} else {
operationNotAvailable(CSEARCH_OPERATION_NO_DEFINITION_MESSAGE);
@ -192,33 +187,7 @@ public class OpenDefinitionAction extends SelectionParseAction implements
if (match instanceof IMatch) {
IMatch theMatch = (IMatch)match;
storage.setFileName(theMatch.getLocation().toOSString());
IMatchLocatable searchLocatable = 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.setLocatable(theMatch.getLocatable());
storage.setResource(ParserUtil.getResourceForFilename(theMatch.getLocation().toOSString()));
break;
}
@ -254,19 +223,17 @@ public class OpenDefinitionAction extends SelectionParseAction implements
ProgressMonitorDialog progressMonitor = new ProgressMonitorDialog(getShell());
progressMonitor.run(true, true, runnable);
int nameOffset = storage.getOffset();
int nameLength = storage.getLength();
if( storage.getResource() != null )
{
clearStatusLine();
open( storage.getResource(), nameOffset, nameLength );
open( storage.getResource(), storage.getLocatable() );
return;
}
String fileName = storage.getFileName();
if (fileName != null){
clearStatusLine();
open( fileName, nameOffset, nameLength);
open( fileName, storage.getLocatable() );
}
} catch(Exception 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.ScannerInfo;
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.search.CSearchMessages;
import org.eclipse.cdt.internal.ui.util.EditorUtility;
@ -553,9 +556,8 @@ public class SelectionParseAction extends Action {
{
private IResource resource;
private String fileName;
private int offset=0;
private int length=0;
private IMatchLocatable locatable;
public final String getFileName() {
return fileName;
}
@ -564,20 +566,14 @@ public class SelectionParseAction extends Action {
public final IResource getResource() {
return resource;
}
public int getLength() {
return length;
}
public int getOffset() {
return offset;
}
public final IMatchLocatable getLocatable() {
return locatable;
}
public void setFileName(String fileName) {
this.fileName = fileName;
}
public void setLength(int length) {
this.length = length;
}
public void setOffset(int offset) {
this.offset = offset;
public void setLocatable(IMatchLocatable locatable) {
this.locatable = locatable;
}
public void setResource(IResource resource) {
this.resource = resource;
@ -589,25 +585,25 @@ public class SelectionParseAction extends Action {
* @param string
* @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 );
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(path);
if( file != null )
{
open( file, offset, length );
open( file, locatable );
return true;
}
ICProject cproject = CoreModel.getDefault().getCModel().getCProject( projectName );
ITranslationUnit unit = CoreModel.getDefault().createTranslationUnitFrom(cproject, path);
if (unit != null) {
setSelectionAtOffset( EditorUtility.openInEditor(unit), offset, length );
setSelectionAtOffset( EditorUtility.openInEditor(unit), locatable );
return true;
}
FileStorage storage = new FileStorage(null, path);
IEditorPart part = EditorUtility.openInEditor(storage);
setSelectionAtOffset(part, offset, length);
setSelectionAtOffset(part, locatable);
return true;
}
@ -618,9 +614,9 @@ public class SelectionParseAction extends Action {
/**
* 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);
setSelectionAtOffset(part, offset, length);
setSelectionAtOffset(part, locatable);
}
/**
@ -628,11 +624,40 @@ public class SelectionParseAction extends Action {
* @param offset
* @param length TODO
*/
protected void setSelectionAtOffset(IEditorPart part, int offset, int length) {
protected void setSelectionAtOffset(IEditorPart part, IMatchLocatable locatable) {
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 {
((AbstractTextEditor) part).selectAndReveal(offset, length);
((AbstractTextEditor) part).selectAndReveal(startOffset, length);
} catch (Exception e) {}
}
}
@ -677,5 +702,5 @@ public class SelectionParseAction extends Action {
public void update() {
setEnabled(getSelectedStringFromEditor() != null);
}
}

View file

@ -205,8 +205,10 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
int tempstartOffset = ((ILineLocatable)searchLocatable).getStartLine();
IDocument doc =textViewer.getDocument();
try {
startOffset = doc.getLineOffset(tempstartOffset);
length=doc.getLineLength(tempstartOffset);
//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
@ -214,7 +216,8 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
//Make sure that there is a real value for the end line
if (tempendOffset>0 && tempendOffset>tempstartOffset){
try {
int endOffset = doc.getLineOffset(tempendOffset);
//See NOTE above
int endOffset = doc.getLineOffset(tempendOffset-1);
length=endOffset - startOffset;
} catch (BadLocationException e) {}
}
@ -225,11 +228,8 @@ public class CSourceHover extends AbstractCEditorTextHover implements ITextHover
}
}
}
}catch (InterruptedException e) {
//
} catch (CModelException e) {
//
}
}catch (InterruptedException e) {}
catch (CModelException e) {}
}
}
return null;