-
Notifications
You must be signed in to change notification settings - Fork 16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add whitespace check capability and ParenPadQuickFix #298
base: master
Are you sure you want to change the base?
Changes from all commits
348c685
32aa9ea
380573a
156dc4d
f2b6bdd
a345f72
33b391c
4dcae0a
8de767e
85ae4db
7bcf764
cd2444b
cd0d3d7
5843bf6
9de7299
bfe328b
dc0d9ee
1c3d306
3527444
5ba919b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright (C) jdneo | ||
|
||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
|
||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
|
||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.shengchen.checkstyle.quickfix; | ||
|
||
import org.eclipse.jface.text.Document; | ||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.text.edits.TextEdit; | ||
|
||
import java.util.function.Predicate; | ||
|
||
public abstract class BaseEditQuickFix implements IQuickFix { | ||
|
||
public abstract TextEdit createTextEdit(IRegion lineInfo, int markerStartOffset, String violationKey, Document doc); | ||
|
||
protected int measureToken(String string, Predicate<Character> tokenPredicate) { | ||
return measureToken(string, 0, tokenPredicate); | ||
} | ||
|
||
protected int measureToken(String string, int from, Predicate<Character> tokenPredicate) { | ||
final int n = string.length(); | ||
for (int i = from; i < n; i++) { | ||
if (!tokenPredicate.test(string.charAt(i))) { | ||
return i - from; | ||
} | ||
} | ||
return n - from; | ||
} | ||
|
||
protected int measureTokenBackwards(String string, Predicate<Character> tokenPredicate) { | ||
return measureTokenBackwards(string, -1, tokenPredicate); | ||
} | ||
|
||
protected int measureTokenBackwards(String string, int from, Predicate<Character> tokenPredicate) { | ||
final int n = string.length(); | ||
if (from == -1) { | ||
from = n - 1; | ||
} | ||
for (int i = from; i >= 0; i--) { | ||
if (!tokenPredicate.test(string.charAt(i))) { | ||
return from - i; | ||
} | ||
} | ||
return from + 1; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright (C) jdneo | ||
|
||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
|
||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
|
||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.shengchen.checkstyle.quickfix; | ||
|
||
public interface IQuickFix { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright (C) jdneo | ||
|
||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
|
||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
|
||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.shengchen.checkstyle.quickfix.whitepace; | ||
|
||
import com.shengchen.checkstyle.quickfix.BaseEditQuickFix; | ||
|
||
import org.eclipse.jface.text.BadLocationException; | ||
import org.eclipse.jface.text.Document; | ||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.text.edits.DeleteEdit; | ||
import org.eclipse.text.edits.InsertEdit; | ||
import org.eclipse.text.edits.TextEdit; | ||
|
||
/** | ||
* Quick fix for | ||
* https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/checks/whitespace/GenericWhitespaceCheck.html | ||
*/ | ||
public class GenericWhitespaceQuickFix extends BaseEditQuickFix { | ||
|
||
@Override | ||
public TextEdit createTextEdit(IRegion lineInfo, int markerStartOffset, String violationKey, Document doc) { | ||
try { | ||
final int fromStartOfLine = markerStartOffset - lineInfo.getOffset(); | ||
final String string = doc.get(lineInfo.getOffset(), lineInfo.getLength()); | ||
|
||
if ("ws.illegalFollow".equals(violationKey)) { | ||
return new InsertEdit(markerStartOffset + 1, " "); | ||
} else if ("ws.notPreceded".equals(violationKey)) { | ||
return new InsertEdit(markerStartOffset, " "); | ||
} else if ("ws.followed".equals(violationKey)) { | ||
final int tokenLength = measureToken(string, fromStartOfLine + 1, Character::isWhitespace); | ||
if (tokenLength > 0) { | ||
return new DeleteEdit(markerStartOffset + 1, tokenLength); | ||
} | ||
} else if ("ws.preceded".equals(violationKey)) { | ||
final int tokenLength = measureTokenBackwards(string, fromStartOfLine - 1, Character::isWhitespace); | ||
if (tokenLength > 0) { | ||
return new DeleteEdit(markerStartOffset - tokenLength, tokenLength); | ||
} | ||
} | ||
|
||
return null; | ||
} catch (BadLocationException e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Copyright (C) jdneo | ||
|
||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* any later version. | ||
|
||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
|
||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
package com.shengchen.checkstyle.quickfix.whitepace; | ||
|
||
import com.shengchen.checkstyle.quickfix.BaseEditQuickFix; | ||
|
||
import org.eclipse.jface.text.BadLocationException; | ||
import org.eclipse.jface.text.Document; | ||
import org.eclipse.jface.text.IRegion; | ||
import org.eclipse.text.edits.DeleteEdit; | ||
import org.eclipse.text.edits.InsertEdit; | ||
import org.eclipse.text.edits.TextEdit; | ||
|
||
/** | ||
* Quick fix for | ||
* https://checkstyle.org/apidocs/com/puppycrawl/tools/checkstyle/checks/whitespace/MethodParamPadCheck.html | ||
*/ | ||
public class MethodParamPadQuickFix extends BaseEditQuickFix { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jdneo when I test this, Checkstyle reports that the "(" is not preceded by whitespace, and the quick fix adds a space before the "(". That seems like it's doing the right thing to me? |
||
|
||
@Override | ||
public TextEdit createTextEdit(IRegion lineInfo, int markerStartOffset, String violationKey, Document doc) { | ||
try { | ||
final int fromStartOfLine = markerStartOffset - lineInfo.getOffset(); | ||
final String string = doc.get(lineInfo.getOffset(), lineInfo.getLength()); | ||
|
||
if ("ws.notPreceded".equals(violationKey)) { | ||
return new InsertEdit(markerStartOffset, " "); | ||
} else if ("ws.preceded".equals(violationKey)) { | ||
final int tokenLength = measureTokenBackwards(string, fromStartOfLine - 1, Character::isWhitespace); | ||
if (tokenLength > 0) { | ||
return new DeleteEdit(markerStartOffset - tokenLength, tokenLength); | ||
} | ||
} | ||
|
||
return null; | ||
} catch (BadLocationException e) { | ||
return null; | ||
} | ||
} | ||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better if we can directly reference the static const defined in the Checkstyle lib, seems it's not easily achievable given the current OSGi framework. 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jdneo yes, I agree. I don't have experience with OSGi or how all of those dependencies are setup so I took the path of least resistance.