Permalink
Browse files

use location inside matches

  • Loading branch information...
1 parent 55f3a17 commit f175032226017217265e3ce3cef5f799c172dfe0 @JPMoresmau committed May 1, 2012
@@ -21,6 +21,7 @@
* @author Thomas ten Cate
*/
public class Location {
+ private IFile ifile;
private String fileName;
private String otherName;
@@ -32,6 +33,7 @@ public Location(JSONObject json) throws JSONException {
public Location(IFile f, JSONArray json) throws JSONException {
this(f.getLocation().toOSString(),json);
+ this.ifile=f;
}
public Location(String fn, JSONArray json) throws JSONException {
@@ -62,6 +64,7 @@ public Location(IFile f, JSONObject json) throws JSONException {
&& (this.otherName == null || this.otherName.length() == 0) ) {
// Default the file name to the Java file resource
this.fileName = f.getLocation().toOSString();
+ this.ifile=f;
}
if (json.optString("no-location").length() == 0) {
@@ -202,4 +205,12 @@ public IFile getIFile(IProject p){
}
return attributes;
}
+
+ public IFile getIFile() {
+ return ifile;
+ }
+
+ public void setIFile(IFile ifile) {
+ this.ifile = ifile;
+ }
}
@@ -19,59 +19,32 @@
*
*/
public class UsageResults {
- private Map<IProject,Map<IFile,Collection<UsageLocation>>> allResults=new HashMap<IProject, Map<IFile,Collection<UsageLocation>>>();
+ private Map<IProject,Map<IFile,Collection<Location>>> allResults=new HashMap<IProject, Map<IFile,Collection<Location>>>();
- public void put(IFile file,Collection<UsageLocation> locs){
+ public void put(IFile file,Collection<Location> locs){
IProject p=file.getProject();
- Map<IFile,Collection<UsageLocation>> m=allResults.get(p);
+ Map<IFile,Collection<Location>> m=allResults.get(p);
if (m==null){
- m=new HashMap<IFile, Collection<UsageLocation>>();
+ m=new HashMap<IFile, Collection<Location>>();
allResults.put(p, m);
}
- Collection<UsageLocation> allLocs=m.get(file);
+ Collection<Location> allLocs=m.get(file);
if (allLocs==null){
- allLocs=new ArrayList<UsageResults.UsageLocation>();
+ allLocs=new ArrayList<Location>();
m.put(file, allLocs);
}
+ for (Location l:locs){
+ l.setIFile(file);
+ }
allLocs.addAll(locs);
}
public Set<IProject> listProjects(){
return allResults.keySet();
}
- public Map<IFile,Collection<UsageLocation>> getUsageInProject(IProject p){
+ public Map<IFile,Collection<Location>> getUsageInProject(IProject p){
return allResults.get(p);
}
- public static class UsageLocation {
- private int startLine;
- private int lengthInLine;
-
- public UsageLocation(int startLine, int lengthInLine) {
- super();
- this.startLine = startLine;
- this.lengthInLine = lengthInLine;
- }
-
- public UsageLocation(Location l) {
- super();
- this.startLine = l.getStartLine();
- this.lengthInLine = Math.max(1, l.getEndLine()-getStartLine());
- }
-
- /**
- * @return the lengthInLine
- */
- public int getLengthInLine() {
- return lengthInLine;
- }
-
- /**
- * @return the startLine
- */
- public int getStartLine() {
- return startLine;
- }
- }
}
@@ -23,7 +23,6 @@
import net.sf.eclipsefp.haskell.buildwrapper.types.Location;
import net.sf.eclipsefp.haskell.buildwrapper.types.Module;
import net.sf.eclipsefp.haskell.buildwrapper.types.UsageResults;
-import net.sf.eclipsefp.haskell.buildwrapper.types.UsageResults.UsageLocation;
import net.sf.eclipsefp.haskell.buildwrapper.util.BWText;
import org.eclipse.core.resources.IFile;
@@ -327,7 +326,7 @@ public UsageResults getModuleReferences(String pkg,String module) throws SQLExce
sb.append(" and m.package=?");
}
PreparedStatement ps=conn.prepareStatement(sb.toString());
- Map<Long,Collection<UsageLocation>> m=new HashMap<Long, Collection<UsageLocation>>();
+ Map<Long,Collection<Location>> m=new HashMap<Long, Collection<Location>>();
try {
ps.setString(1, module);
if (pkg!=null){
@@ -337,9 +336,9 @@ public UsageResults getModuleReferences(String pkg,String module) throws SQLExce
try {
while (rs.next()){
long fileid=rs.getLong(1);
- Collection<UsageLocation> locs=m.get(fileid);
+ Collection<Location> locs=m.get(fileid);
if (locs==null){
- locs=new ArrayList<UsageResults.UsageLocation>();
+ locs=new ArrayList<Location>();
m.put(fileid,locs);
}
//IProject p=ResourcesPlugin.getWorkspace().getRoot().getProject(project);
@@ -348,7 +347,7 @@ public UsageResults getModuleReferences(String pkg,String module) throws SQLExce
String loc=rs.getString(2);
try {
Location l=new net.sf.eclipsefp.haskell.buildwrapper.types.Location("", new JSONArray(loc));
- locs.add(new UsageLocation(l));
+ locs.add(l);
} catch (JSONException je){
BuildWrapperPlugin.logError(je.getLocalizedMessage(), je);
@@ -11,6 +11,7 @@
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
+import net.sf.eclipsefp.haskell.buildwrapper.types.Location;
import net.sf.eclipsefp.haskell.buildwrapper.types.UsageResults;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
@@ -53,9 +54,10 @@ public void inputChanged( final Viewer arg0, final Object arg1, final Object arg
Set<IProject> ps=results.listProjects();
roots=ps.toArray( new Object[ps.size()] );
for (IProject p:ps){
- Map<IFile,Collection<UsageResults.UsageLocation>> m=results.getUsageInProject( p );
+ Map<IFile,Collection<Location>> m=results.getUsageInProject( p );
for (IFile f:m.keySet()){
- uiresults.put( f, new ArrayList<Object>(m.get( f )));
+ Collection<Location> uls=m.get( f );
+ uiresults.put( f, new ArrayList<Object>(uls));
IContainer parent=f.getParent();
Object child=f;
while (!(parent instanceof IWorkspaceRoot)){
@@ -5,12 +5,22 @@
*/
package net.sf.eclipsefp.haskell.ui.internal.search;
+import net.sf.eclipsefp.haskell.buildwrapper.types.Location;
+import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
import net.sf.eclipsefp.haskell.ui.internal.views.projectexplorer.HaskellResourceExtensionLP;
+import org.eclipse.jface.text.IDocument;
+import org.eclipse.jface.text.IRegion;
+import org.eclipse.jface.text.Region;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.search.ui.ISearchResult;
import org.eclipse.search.ui.text.AbstractTextSearchViewPage;
+import org.eclipse.search.ui.text.Match;
+import org.eclipse.ui.IWorkbenchPage;
+import org.eclipse.ui.PartInitException;
+import org.eclipse.ui.editors.text.TextFileDocumentProvider;
import org.eclipse.ui.model.WorkbenchViewerComparator;
+import org.eclipse.ui.texteditor.IDocumentProvider;
/**
@@ -45,7 +55,6 @@
@Override
protected void elementsChanged( final Object[] paramArrayOfObject ) {
getViewer().setInput( lastResults);
- //getViewer().refresh();
}
/* (non-Javadoc)
@@ -83,8 +92,18 @@ public void setInput( final ISearchResult newSearch, final Object viewState ) {
*/
@Override
protected void clear() {
- // NOOP
+ lastResults=null;
+
+ }
+ /* (non-Javadoc)
+ * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#showMatch(org.eclipse.search.ui.text.Match, int, int, boolean)
+ */
+ @Override
+ protected void showMatch( final Match match, final int currentOffset, final int currentLength,
+ final boolean activate ) throws PartInitException {
+ IWorkbenchPage page = getSite().getPage();
+ openAndSelect( page, ((Location)match.getElement()).getIFile(), currentOffset, currentLength, activate );
}
/* (non-Javadoc)
@@ -107,4 +126,26 @@ protected void configureTableViewer( final TableViewer tableViewer ) {
}
+ /* (non-Javadoc)
+ * @see org.eclipse.search.ui.text.AbstractTextSearchViewPage#getCurrentMatchLocation(org.eclipse.search.ui.text.Match)
+ */
+ @Override
+ public IRegion getCurrentMatchLocation( final Match match ) {
+ IDocumentProvider prov=new TextFileDocumentProvider();
+ Location loc=(Location)match.getElement();
+ try {
+ prov.connect( loc.getIFile() );
+ try {
+ IDocument doc=prov.getDocument( loc.getIFile() );
+ int off1=loc.getStartOffset( doc );
+ return new Region(off1,loc.getLength( doc ));
+ } finally {
+ prov.disconnect( loc.getIFile() );
+ }
+ } catch (Exception ce){
+ HaskellUIPlugin.log( ce );
+ }
+ return super.getCurrentMatchLocation( match );
+ }
+
}
@@ -7,9 +7,12 @@
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import net.sf.eclipsefp.haskell.buildwrapper.types.Location;
import net.sf.eclipsefp.haskell.buildwrapper.types.UsageResults;
+import net.sf.eclipsefp.haskell.ui.internal.editors.haskell.HaskellEditor;
import net.sf.eclipsefp.haskell.ui.internal.util.UITexts;
import net.sf.eclipsefp.haskell.ui.util.HaskellUIImages;
import net.sf.eclipsefp.haskell.ui.util.IImageNames;
@@ -22,6 +25,7 @@
import org.eclipse.search.ui.text.IFileMatchAdapter;
import org.eclipse.search.ui.text.Match;
import org.eclipse.search.ui.text.MatchEvent;
+import org.eclipse.ui.IEditorPart;
/**
@@ -34,6 +38,7 @@
// private final List<ISearchResultListener> ll=new ArrayList<ISearchResultListener>();
private UsageResults results=null;
+ private final Map<IFile,Match[]> matchByFile=new HashMap<IFile, Match[]>();
public UsageSearchResult(final ISearchQuery query) {
super();
@@ -85,17 +90,20 @@ public UsageResults getResults() {
public void setResults( final UsageResults results ) {
this.results = results;
-
+ matchByFile.clear();
final List<Match> matches=new ArrayList<Match>();
if (this.results!=null){
for (IProject p:this.results.listProjects()){
- Map<IFile,Collection<UsageResults.UsageLocation>> m=this.results.getUsageInProject( p );
+ Map<IFile,Collection<Location>> m=this.results.getUsageInProject( p );
for (IFile f:m.keySet()){
- for (UsageResults.UsageLocation loc:m.get( f )){
- Match match=new Match(loc,Match.UNIT_LINE,loc.getStartLine(),loc.getLengthInLine());
+ List<Match> myMatches=new ArrayList<Match>();
+ for (Location loc:m.get( f )){
+ Match match=new Match(loc,Match.UNIT_LINE,loc.getStartLine()-1,1);
addMatch( match );
- matches.add(match);
+ myMatches.add(match);
}
+ matchByFile.put( f, myMatches.toArray( new Match[myMatches.size()] ) );
+ matches.addAll(myMatches);
}
}
}
@@ -128,17 +136,60 @@ public void setResults( final UsageResults results ) {
*/
@Override
public IFileMatchAdapter getFileMatchAdapter() {
- // TODO Auto-generated method stub
- return null;
+ return new FileMatchAdapter() ;
}
/* (non-Javadoc)
* @see org.eclipse.search.ui.text.AbstractTextSearchResult#getEditorMatchAdapter()
*/
@Override
public IEditorMatchAdapter getEditorMatchAdapter() {
- // TODO Auto-generated method stub
- return null;
+ return new EditorMatchAdapter();
+ }
+
+ private static class FileMatchAdapter implements IFileMatchAdapter{
+ @Override
+ public IFile getFile( final Object paramObject ) {
+ if (paramObject instanceof Location){
+ return ((Location)paramObject).getIFile();
+ }
+ return null;
+ }
+
+ @Override
+ public Match[] computeContainedMatches(
+ final AbstractTextSearchResult paramAbstractTextSearchResult, final IFile paramIFile ) {
+ return ((UsageSearchResult)paramAbstractTextSearchResult).matchByFile.get( paramIFile );
+ }
+ }
+
+ private static class EditorMatchAdapter implements IEditorMatchAdapter {
+ /* (non-Javadoc)
+ * @see org.eclipse.search.ui.text.IEditorMatchAdapter#isShownInEditor(org.eclipse.search.ui.text.Match, org.eclipse.ui.IEditorPart)
+ */
+ @Override
+ public boolean isShownInEditor( final Match paramMatch,
+ final IEditorPart paramIEditorPart ) {
+ if (paramIEditorPart instanceof HaskellEditor){
+ IFile f=((Location)paramMatch.getElement()).getIFile();
+ return f.equals(((HaskellEditor)paramIEditorPart).findFile());
+ }
+ return false;
+ }
+
+ /* (non-Javadoc)
+ * @see org.eclipse.search.ui.text.IEditorMatchAdapter#computeContainedMatches(org.eclipse.search.ui.text.AbstractTextSearchResult, org.eclipse.ui.IEditorPart)
+ */
+ @Override
+ public Match[] computeContainedMatches(
+ final AbstractTextSearchResult paramAbstractTextSearchResult,
+ final IEditorPart paramIEditorPart ) {
+ if (paramIEditorPart instanceof HaskellEditor){
+ IFile f=((HaskellEditor)paramIEditorPart).findFile();
+ return ((UsageSearchResult)paramAbstractTextSearchResult).matchByFile.get( f );
+ }
+ return null;
+ }
}
}
Oops, something went wrong.

0 comments on commit f175032

Please sign in to comment.