Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

hlint adds brackets sometimes

  • Loading branch information...
commit 675f4b45766dec2892f23d62546cd7b650c502e4 1 parent a8eb69a
@JPMoresmau authored
View
39 net.sf.eclipsefp.haskell.hlint.test/src/net/sf/eclipsefp/haskell/hlint/HLintFixerTest.java
@@ -6,7 +6,9 @@
package net.sf.eclipsefp.haskell.hlint;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
import net.sf.eclipsefp.haskell.hlint.HLintFixer.HLintFix;
import org.junit.Test;
@@ -30,6 +32,7 @@ public void testListComprehension(){
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);
+ assertTrue(fix.isFullMatch());
assertEquals(110,fix.getLength());
String newDoc=doc.substring(0,7)+fix.getValue()+doc.substring(7+fix.getLength());
assertEquals(" ([CCLibrary\n (PD.buildable $ PD.libBuildInfo $ fromJust (PD.library pd)) \n | isJust (PD.library pd)]) ++",newDoc);
@@ -46,6 +49,7 @@ public void testRedundantBracket(){
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);
+ assertTrue(fix.isFullMatch());
assertEquals(60,fix.getLength());
String newDoc=doc.substring(0,47)+fix.getValue()+doc.substring(47+fix.getLength());
assertEquals(" in DM.assocs $ DM.fromListWith (++) $ ((map (\\ (a, b) -> (a, [b])) cpkgs) ++\n map (\\ (a, _) -> (a, [])) pkgs)",newDoc);
@@ -62,6 +66,7 @@ public void testRedundantBracketSpace(){
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);
+ assertTrue(fix.isFullMatch());
assertEquals(60,fix.getLength());
String newDoc=doc.substring(0,47)+fix.getValue()+doc.substring(47+fix.getLength());
assertEquals(" in DM.assocs $ DM.fromListWith (++) $ ((map (\\ (a, b) -> (a, [b])) cpkgs) ++\n map (\\ (a, _) -> (a, [])) pkgs )",newDoc);
@@ -77,6 +82,7 @@ public void testDiscardComment(){
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);
+ assertTrue(fix.isFullMatch());
assertEquals(98,fix.getLength());
String newDoc=doc.substring(0,21)+fix.getValue()+doc.substring(21+fix.getLength());
assertEquals("copyFileFull src tgt=copyFile src tgt",newDoc);
@@ -92,10 +98,43 @@ public void testAddSpace(){
String doc="case mcbi of\n Just(cbi)->do";
HLintFix fix=HLintFixer.fix(doc, 21, sug);
assertNotNull(fix);
+ assertTrue(fix.isFullMatch());
assertEquals(5,fix.getLength());
String newDoc=doc.substring(0,21)+fix.getValue()+doc.substring(21+fix.getLength());
assertEquals("case mcbi of\n Just cbi->do",newDoc);
}
+
+ @Test
+ public void testExtraBrackets(){
+ String t0="case ls of\n (loc : (line : (col : []))) -> (Just $\n BWNote BWError (dropWhile isSpace aft)\n (BWLocation loc (read line) (read col)))\n _ -> Nothing";
+ String t1="case ls of\n (loc : (line : (col : []))) -> Just $\n BWNote BWError (dropWhile isSpace aft)\n (BWLocation loc (read line) (read col))\n _ -> Nothing";
+ Suggestion sug=new Suggestion();
+ sug.setPre(new CodeModificationText(t0));
+ sug.setPost(new CodeModificationText(t1));
+ sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 338, 28));
+ sug.setMessage("Warning: Redundant bracket");
+ 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());
+ assertNotNull(fix);
+ assertEquals(195,fix.getLength());
+ String newDoc=doc.substring(0,27)+fix.getValue()+doc.substring(27+fix.getLength());
+ assertEquals(" in case ls of\n (loc : (line : (col : []))) -> Just $\n BWNote BWError (dropWhile isSpace aft)\n (BWLocation loc (read line) (read col))\n _ -> Nothing",newDoc);
+ }
+
+ @Test
+ public void testNoMatch(){
+ Suggestion sug=new Suggestion();
+ sug.setPre(new CodeModificationText("(aaa)"));
+ sug.setPost(new CodeModificationText("cbi"));
+ sug.setLocation(new SourceLocation("src/Language/Haskell/BuildWrapper/Cabal.hs", 537, 48));
+ sug.setMessage("Warning: Redundant bracket");
+ String doc="case mcbi of\n Just(cbi)->do";
+ HLintFix fix=HLintFixer.fix(doc, 21, sug);
+ assertNotNull(fix);
+ assertFalse(fix.isFullMatch());
+ }
+
}
View
19 net.sf.eclipsefp.haskell.hlint/src/net/sf/eclipsefp/haskell/hlint/HLintFixer.java
@@ -27,6 +27,11 @@
private String value="";
/**
+ * have we fully matched the input
+ */
+ private boolean fullMatch=false;
+
+ /**
* @return the length
*/
public int getLength() {
@@ -39,6 +44,13 @@ public int getLength() {
public String getValue() {
return value;
}
+
+ /**
+ * @return the fullMatch
+ */
+ public boolean isFullMatch() {
+ return fullMatch;
+ }
}
/**
@@ -53,7 +65,10 @@ public static HLintFix fix(String doc, int offset,Suggestion s){
if (s.getPre().getType().equals(CodeModificationType.TEXT)){
String txt=((CodeModificationText)s.getPre()).getText();
int offset2=matchIgnoreSpace(doc, offset, txt, 0);
+ fix.fullMatch=offset2>-1;
fix.length=offset2-offset;
+ } else {
+ fix.fullMatch=true;
}
if (s.getPost().getType().equals(CodeModificationType.TEXT)){
fix.value=((CodeModificationText)s.getPost()).getText().trim();
@@ -87,6 +102,10 @@ private static int matchIgnoreSpace(String doc,int offset,String txt, int txtIdx
char c2=txt.charAt(txtIdx2);
if (c1==c2){
return matchIgnoreSpace(doc, offset2+1, txt, txtIdx2+1);
+ } else if (c2=='(' || c2==')'){ // hlint sometimes adds extra brackets in the text it tells you to replace
+ return matchIgnoreSpace(doc, offset2, txt, txtIdx2+1);
+ } else {
+ return -1;
}
}
offset=offset2;
View
8 net.sf.eclipsefp.haskell.ui/src/net/sf/eclipsefp/haskell/ui/internal/resolve/HLintResolution.java
@@ -46,9 +46,11 @@ public ICompletionProposal getCompletionProposal( final IMarker marker,
try {
int offset=document.getLineOffset( line-1 )+s.getLocation().getColumn()-1;
HLintFix fix=HLintFixer.fix( document.get(), offset, s );
- 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 (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);
+ }
} catch( BadLocationException ex ) {
HaskellUIPlugin.log( ex );
}
Please sign in to comment.
Something went wrong with that request. Please try again.