Permalink
Browse files

duplication warning cannot be automatically fixed

  • Loading branch information...
1 parent 2a9635b commit b0ab681d3af421b6cb44efa3bb134821e850c385 @JPMoresmau committed Feb 29, 2012
@@ -106,7 +106,7 @@ static void createMarker( final IResource resource, final Suggestion s )
if( s.getSeverity() != Severity.IGNORE ) {
IMarker marker = resource
.createMarker( HaskellCorePlugin.ID_HLINT_MARKER );
- marker.setAttribute( IMarker.MESSAGE, s.getMessage() );
+ marker.setAttribute( IMarker.MESSAGE, s.getMarkerText() );
marker.setAttribute( IMarker.SEVERITY,getMarkerSeverity(s.getSeverity()) );
marker.setAttribute( IMarker.LINE_NUMBER, s.getLocation().getLine() );
marker.setAttribute( HaskellCorePlugin.ATT_HLINT_SUGGESTION, s.toString() );
@@ -28,7 +28,7 @@ public void testListComprehension(){
sug.setPost(new CodeModificationText("[CCLibrary\n (PD.buildable $ PD.libBuildInfo $ fromJust (PD.library pd)) \n | isJust (PD.library pd)]"));
sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 560, 8));
sug.setMessage("Warning: Use list comprehension");
-
+ assertTrue(HLintFixer.canFix(sug));
String doc=" (if isJust (PD.library pd) then [CCLibrary (PD.buildable $ PD.libBuildInfo $ fromJust (PD.library pd))] else []) ++";
HLintFix fix=HLintFixer.fix(doc, 7, sug);
assertNotNull(fix);
@@ -45,7 +45,7 @@ public void testRedundantBracket(){
sug.setPost(new CodeModificationText(" (map (\\ (a, b) -> (a, [b])) cpkgs) ++\n map (\\ (a, _) -> (a, [])) pkgs"));
sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 537, 48));
sug.setMessage("Warning: Redundant bracket");
-
+ assertTrue(HLintFixer.canFix(sug));
String doc=" in DM.assocs $ DM.fromListWith (++) $ ((map (\\(a,b)->(a,[b])) cpkgs) ++ (map (\\(a,_)->(a,[])) pkgs))";
HLintFix fix=HLintFixer.fix(doc, 47, sug);
assertNotNull(fix);
@@ -62,7 +62,7 @@ public void testRedundantBracketSpace(){
sug.setPost(new CodeModificationText(" (map (\\ (a, b) -> (a, [b])) cpkgs) ++\n map (\\ (a, _) -> (a, [])) pkgs"));
sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 537, 48));
sug.setMessage("Warning: Redundant bracket");
-
+ assertTrue(HLintFixer.canFix(sug));
String doc=" in DM.assocs $ DM.fromListWith (++) $ ((map (\\(a,b)->(a,[b])) cpkgs) ++ (map (\\(a,_)->(a,[])) pkgs) )";
HLintFix fix=HLintFixer.fix(doc, 47, sug);
assertNotNull(fix);
@@ -79,6 +79,7 @@ public void testDiscardComment(){
sug.setPost(new CodeModificationText("copyFile src tgt"));
sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 537, 48));
sug.setMessage("Warning: Redundant do");
+ assertTrue(HLintFixer.canFix(sug));
String doc="copyFileFull src tgt=do\n --createDirectoryIfMissing True (takeDirectory tgt)\n --putStrLn tgt\n copyFile src tgt";
HLintFix fix=HLintFixer.fix(doc, 21, sug);
assertNotNull(fix);
@@ -95,6 +96,7 @@ public void testAddSpace(){
sug.setPost(new CodeModificationText("cbi"));
sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 537, 48));
sug.setMessage("Warning: Redundant bracket");
+ assertTrue(HLintFixer.canFix(sug));
String doc="case mcbi of\n Just(cbi)->do";
HLintFix fix=HLintFixer.fix(doc, 21, sug);
assertNotNull(fix);
@@ -113,6 +115,7 @@ public void testExtraBrackets(){
sug.setPost(new CodeModificationText(t1));
sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 338, 28));
sug.setMessage("Warning: Redundant bracket");
+ assertTrue(HLintFixer.canFix(sug));
String doc=" in case ls of\n (loc:line:col:[])-> (Just $ BWNote BWError (dropWhile isSpace aft) (BWLocation loc (read line) (read col)))\n _ -> Nothing";
HLintFix fix=HLintFixer.fix(doc, 27, sug);
assertTrue(fix.isFullMatch());
@@ -129,6 +132,7 @@ public void testNoMatch(){
sug.setPost(new CodeModificationText("cbi"));
sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 537, 48));
sug.setMessage("Warning: Redundant bracket");
+ assertTrue(HLintFixer.canFix(sug));
String doc="case mcbi of\n Just(cbi)->do";
HLintFix fix=HLintFixer.fix(doc, 21, sug);
assertNotNull(fix);
@@ -5,6 +5,8 @@
*/
package net.sf.eclipsefp.haskell.hlint;
+import java.util.Locale;
+
/**
* fixes HLint warning
* @author JP Moresmau
@@ -53,6 +55,20 @@ public boolean isFullMatch() {
}
}
+ public static boolean canFix(Suggestion s){
+ if (s.getPre().getType().equals(CodeModificationType.TEXT)){
+ if (s.getPost().getType().equals(CodeModificationType.TEXT)){
+ String p=((CodeModificationText)s.getPost()).getText().trim();
+ // we cannot replace automatically when we're just told to combine with another line
+ if (p.length()>0 && p.toLowerCase(Locale.ENGLISH).startsWith("combine with") && Character.isDigit(p.charAt(p.length()-1))){
+ return false;
+ }
+ }
+ return true;
+ }
+ return false;
+ }
+
/**
* fixes the given string
* @param doc the document content
@@ -6,6 +6,9 @@
import java.io.Serializable;
+import net.sf.eclipsefp.haskell.hlint.util.HLintText;
+import net.sf.eclipsefp.haskell.util.PlatformUtil;
+
import org.json.JSONException;
import org.json.JSONObject;
@@ -75,6 +78,33 @@ public String getPostText(){
return null;
}
+ /**
+ * get the text suitable for the marker description
+ * @return
+ */
+ public String getMarkerText(){
+ // we can't fix automatically, let's give as much info as we can
+ if (!HLintFixer.canFix(this)){
+ String pre=getPreText();
+ String post=getPostText();
+ if (pre!=null && post!=null){
+ String nl=" "+PlatformUtil.NL; // leave space for problems view visibility
+ StringBuilder sb=new StringBuilder();
+ sb.append(getMessage());
+ sb.append(nl);
+ sb.append(HLintText.suggestion_found);
+ sb.append(nl);
+ sb.append(pre);
+ sb.append(nl);
+ sb.append(HLintText.suggestion_post);
+ sb.append(nl);
+ sb.append(post);
+ return sb.toString();
+ }
+ }
+ return getMessage();
+ }
+
public String toString() {
JSONObject obj=new JSONObject();
try {
@@ -9,6 +9,8 @@
import net.sf.eclipsefp.haskell.browser.items.Module;
import net.sf.eclipsefp.haskell.buildwrapper.types.GhcMessages;
import net.sf.eclipsefp.haskell.core.HaskellCorePlugin;
+import net.sf.eclipsefp.haskell.hlint.HLintFixer;
+import net.sf.eclipsefp.haskell.hlint.Suggestion;
import net.sf.eclipsefp.haskell.ui.HaskellUIPlugin;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
@@ -182,7 +184,11 @@ else if (msgL.indexOf( GhcMessages.NOT_IN_SCOPE_START )>-1){
private IMarkerResolution getHLintResolution(final IMarker marker) {
try {
if (marker.getType().equals(HaskellCorePlugin.ID_HLINT_MARKER)){
- return new HLintResolution();
+ Suggestion s=new Suggestion();
+ s.fromString( marker.getAttribute( HaskellCorePlugin.ATT_HLINT_SUGGESTION,"" ));
+ if (HLintFixer.canFix( s )){
+ return new HLintResolution();
+ }
}
}catch (CoreException ce){
HaskellUIPlugin.log( ce );
@@ -48,8 +48,10 @@ public ICompletionProposal getCompletionProposal( final IMarker marker,
HLintFix fix=HLintFixer.fix( document.get(), offset, s );
if (fix.isFullMatch()){
String add=getAdditionalInfo(s) ;
- String label=NLS.bind( UITexts.resolve_hlint_explain,add);
- return new MarkerCompletionProposal( fix.getValue(), offset, fix.getLength(), fix.getLength(),label,marker,add);
+ if (add!=null){
+ String label=NLS.bind( UITexts.resolve_hlint_explain,add);
+ return new MarkerCompletionProposal( fix.getValue(), offset, fix.getLength(), fix.getLength(),label,marker,add);
+ }
}
} catch( BadLocationException ex ) {
HaskellUIPlugin.log( ex );

0 comments on commit b0ab681

Please sign in to comment.