Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

rename locals if not found in usage

  • Loading branch information...
commit 30769f95759a0c740e7e865ff871638f2ce70575 1 parent d0628fd
@JPMoresmau authored
View
3  docs/releasenotes/net.sf.eclipsefp.haskell_2.4.3.txt
@@ -21,7 +21,8 @@ Features:
- Cabal Packages view shows a green tick on installed packages and versions when you display the list of all hackage packages.
- Distinguish comments/haddock/pragmas in syntax highlighting (https://github.com/JPMoresmau/eclipsefp/issues/44)
- Use project root as HLint working folder
- - Organize import on single file: only keep actually used imports
+ - Organize imports on single file: only keep actually used imports
+ - Refactor->rename can rename local bindings
Internal:
View
33 net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/editors/haskell/HaskellEditor.java
@@ -854,6 +854,39 @@ public void handleNameDefs( final Collection<NameDef> names ) {
return names;
}
+ public Location getOutlineSpan(final int offset){
+ Location odL=null;
+
+ IDocument doc=getDocument();
+ if (doc!=null){
+ try {
+ int line=doc.getLineOfOffset( offset );
+ int col=offset-doc.getLineOffset( line );
+ OutlineResult or=getLastOutlineResult();
+ if (or!=null){
+
+ for (OutlineDef od:or.getOutlineDefs()){
+ Location l=od.getLocation();
+ if (l.contains( line, col )){
+ odL=l;
+ for (OutlineDef odc:od.getChildren()){
+ Location lc=odc.getLocation();
+ if (lc.contains( line, col )){
+ odL=lc;
+ break;
+ }
+ }
+ break;
+ }
+ }
+ }
+ } catch (BadLocationException ble){
+ HaskellUIPlugin.log( ble );
+ }
+ }
+ return odL;
+ }
+
// Interface methods for IScionEventListener
// /**
View
41 ...i/src/net/sf/eclipsefp/haskell/ui/internal/editors/haskell/codeassist/HaskellContentAssistProcessor.java
@@ -34,8 +34,6 @@
import net.sf.eclipsefp.haskell.buildwrapper.types.CabalPackage;
import net.sf.eclipsefp.haskell.buildwrapper.types.ImportDef;
import net.sf.eclipsefp.haskell.buildwrapper.types.Location;
-import net.sf.eclipsefp.haskell.buildwrapper.types.OutlineDef;
-import net.sf.eclipsefp.haskell.buildwrapper.types.OutlineResult;
import net.sf.eclipsefp.haskell.buildwrapper.types.ThingAtPoint;
import net.sf.eclipsefp.haskell.core.cabalmodel.CabalSyntax;
import net.sf.eclipsefp.haskell.core.cabalmodel.PackageDescription;
@@ -628,40 +626,17 @@ private static void addLocals(final HaskellEditor editor,final IProject project,
if (editor!=null){
BWFacade f=BuildWrapperPlugin.getFacade(project) ;
if (f!=null){
- try {
- int line=doc.getLineOfOffset( offset );
- int col=offset-doc.getLineOffset( line );
- OutlineResult or=editor.getLastOutlineResult();
- if (or!=null){
- Location odL=null;
- for (OutlineDef od:or.getOutlineDefs()){
- Location l=od.getLocation();
- if (l.contains( line, col )){
- odL=l;
- for (OutlineDef odc:od.getChildren()){
- Location lc=odc.getLocation();
- if (lc.contains( line, col )){
- odL=lc;
- break;
- }
- }
- break;
- }
- }
- if (odL!=null){
- List<ThingAtPoint> taps=f.getLocals( theFile,odL);
- for (ThingAtPoint t:taps){
- Local func=new Local("",t.getName(),t.getType());
- if (!importeds.containsKey( t.getName() )){
- importeds.put(t.getName(),func);
- }
+ Location odL=editor.getOutlineSpan( offset );
+ if (odL!=null){
+ List<ThingAtPoint> taps=f.getLocals( theFile,odL);
+ for (ThingAtPoint t:taps){
+ Local func=new Local("",t.getName(),t.getType());
+ if (!importeds.containsKey( t.getName() )){
+ importeds.put(t.getName(),func);
}
}
}
- } catch (BadLocationException ble){
- HaskellUIPlugin.log( ble );
- }
- }
+ }
}
}
View
31 net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/refactoring/RefDelegate.java
@@ -59,8 +59,7 @@ public ThingAtPoint getThingAtPoint(){
try {
Location l = new Location( info.getSourceFile().getLocation().toOSString(),
haskellEditor.getDocument(), new Region( info.getOffset(), 0 ) );
- BWFacade f=BuildWrapperPlugin.getFacade( info.getSourceFile().getProject() );
- tap=f.getThingAtPoint( info.getSourceFile(), l );
+ return getThingAtPoint( l );
} catch (Exception e){
HaskellUIPlugin.log( e );
}
@@ -69,6 +68,34 @@ public ThingAtPoint getThingAtPoint(){
return tap;
}
+ /**
+ * get the location of the rename
+ * @return
+ */
+ public Location getLocation(){
+ if (info.getTargetEditor() instanceof HaskellEditor){
+ final HaskellEditor haskellEditor= (HaskellEditor)info.getTargetEditor();
+ try {
+ Location l = new Location( info.getSourceFile().getLocation().toOSString(),
+ haskellEditor.getDocument(), new Region( info.getOffset(), 0 ) );
+ return l;
+ } catch (Exception e){
+ HaskellUIPlugin.log( e );
+ }
+ }
+ return null;
+ }
+
+ public ThingAtPoint getThingAtPoint(final Location l){
+ if (tap==null){
+ if (info.getTargetEditor() instanceof HaskellEditor){
+ BWFacade f=BuildWrapperPlugin.getFacade( info.getSourceFile().getProject() );
+ tap=f.getThingAtPoint( info.getSourceFile(), l );
+ }
+ }
+ return tap;
+ }
+
abstract RefactoringStatus checkFinalConditions(
final IProgressMonitor pm,
final CheckConditionsContext ctxt );
View
32 net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/refactoring/RenameDelegate.java
@@ -3,6 +3,9 @@
// version 1.0 (EPL). See http://www.eclipse.org/legal/epl-v10.html
package net.sf.eclipsefp.haskell.ui.internal.refactoring;
+import java.util.List;
+import net.sf.eclipsefp.haskell.buildwrapper.types.Location;
+import net.sf.eclipsefp.haskell.buildwrapper.types.Occurrence;
import net.sf.eclipsefp.haskell.buildwrapper.types.ThingAtPoint;
import net.sf.eclipsefp.haskell.buildwrapper.usage.UsageQueryFlags;
import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
@@ -42,8 +45,6 @@ public RenameDelegate( final RefInfo info ) {
}
}
-
-
@Override
RefactoringStatus checkFinalConditions( final IProgressMonitor pm,
final CheckConditionsContext ctxt ) {
@@ -93,14 +94,25 @@ private CompositeChange createRenameChange(final IProgressMonitor pm) {
if (info.getTargetEditor() instanceof HaskellEditor){
final HaskellEditor haskellEditor= (HaskellEditor)info.getTargetEditor();
try {
- ThingAtPoint tap=getThingAtPoint();
- if (tap!=null){
- UsageQuery uq=ReferencesWorkspaceHandler.getUsageQuery( haskellEditor, getProject(), tap );
- uq.setScopeFlags( UsageQueryFlags.SCOPE_ALL );
- uq.run( pm );
- UsageSearchResult usr=(UsageSearchResult)uq.getSearchResult();
- CompositeChange cc=ChangeCreator.getReferencesChange( usr.getResults(), tap.getName(), getNewName() );
- return cc;
+ Location l=getLocation();
+ if (l!=null){
+ ThingAtPoint tap=getThingAtPoint(l);
+ if (tap!=null){
+ UsageQuery uq=ReferencesWorkspaceHandler.getUsageQuery( haskellEditor, getProject(), tap );
+ uq.setScopeFlags( UsageQueryFlags.SCOPE_ALL );
+ uq.run( pm );
+ UsageSearchResult usr=(UsageSearchResult)uq.getSearchResult();
+ if (usr.getResults().getSize()>0){
+ CompositeChange cc=ChangeCreator.getReferencesChange( usr.getResults(), tap.getName(), getNewName() );
+ return cc;
+ } else {
+ // nothing found: change locally
+ List<Occurrence> occs=haskellEditor.getTokenScanner().getOccurrences( l.getStartOffset( haskellEditor.getDocument() ) );
+ Location spanLocation=haskellEditor.getOutlineSpan( getLocation().getStartOffset( haskellEditor.getDocument() ) );
+ CompositeChange cc=ChangeCreator.getLocalReferencesChange(haskellEditor.findFile(),spanLocation, occs, tap.getName(), getNewName() );
+ return cc;
+ }
+ }
}
} catch (Exception e){
HaskellUIPlugin.log( e );
View
41 ...clipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/refactoring/participants/ChangeCreator.java
@@ -8,6 +8,8 @@
import java.util.List;
import java.util.Map;
import net.sf.eclipsefp.haskell.buildwrapper.BuildWrapperPlugin;
+import net.sf.eclipsefp.haskell.buildwrapper.types.Location;
+import net.sf.eclipsefp.haskell.buildwrapper.types.Occurrence;
import net.sf.eclipsefp.haskell.buildwrapper.types.SearchResultLocation;
import net.sf.eclipsefp.haskell.buildwrapper.types.UsageResults;
import net.sf.eclipsefp.haskell.core.util.ResourceUtil;
@@ -117,6 +119,45 @@ public static CompositeChange getReferencesChange(final UsageResults ur,final St
}
+ /**
+ * change occurrence locally
+ * @param f the file
+ * @param filter the location of the outline span, only occurrences within this location are used
+ * @param occs the occurences
+ * @param oldName the old name
+ * @param newName the new name
+ * @return the change
+ */
+ public static CompositeChange getLocalReferencesChange(final IFile f,final Location filter,final List<Occurrence> occs,final String oldName,final String newName){
+ CompositeChange referencesChange = new CompositeChange( UITexts.updateReferences );
+
+
+ TextFileChange importChanges = new TextFileChange( UITexts.updateReferences,f);
+ MultiTextEdit multiEdit = new MultiTextEdit();
+ IDocumentProvider prov=new TextFileDocumentProvider();
+ try {
+ prov.connect( f );
+ IDocument doc=prov.getDocument( f );
+ try {
+ for (Occurrence occ: occs){
+ if (filter.contains( occ.getLine() , occ.getColumn())){
+ int offset=doc.getLineOffset( occ.getLine()-1 )+occ.getColumn();
+ multiEdit.addChild( new ReplaceEdit( offset, oldName.length(), newName ));
+ }
+ }
+ } finally {
+ prov.disconnect( f );
+ }
+ } catch (Exception ce){
+ HaskellUIPlugin.log( ce );
+ }
+ importChanges.setEdit( multiEdit );
+ referencesChange.add( importChanges );
+
+ return referencesChange;
+
+ }
+
public static Change createRenameMoveChange(final IFile file, final IPath newPath,
final boolean updateReferences, final String title) {
String oldModule = ResourceUtil.getModuleName( file );
Please sign in to comment.
Something went wrong with that request. Please try again.