Skip to content
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

Python autoindent support and fix to Python Fold Parser #508

Merged
merged 3 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,16 @@ public List<Fold> getFolds(RSyntaxTextArea textArea) {
currentNextFoldStart = t.getOffset() + leadingWhiteSpaceCount;
}

// need to create a fold of some sort
else if (leadingWhiteSpaceCount > currentLeadingWhiteSpaceCount) {

// look forward if it is a line continuation
while (tokenHasLineContinuation(t) && (line < lineCount)) {
line ++;
t = textArea.getTokenListForLine(line);
//currentNextFoldStart = t.getOffset();
}

if (currentFold != null) {
currentFold = currentFold.createChild(FoldType.CODE,
currentNextFoldStart);
Expand Down Expand Up @@ -80,10 +88,8 @@ else if (leadingWhiteSpaceCount > currentLeadingWhiteSpaceCount) {
int endOffs = t.getEndOffset() - 1;

boolean foundBlock = false;
while (!foldStartLeadingWhiteSpaceCounts.isEmpty() &&
while (currentFold != null && !foldStartLeadingWhiteSpaceCounts.isEmpty() &&
foldStartLeadingWhiteSpaceCounts.peek() >= leadingWhiteSpaceCount) {
// IntelliJ can't tell, but it's not possible for currentFold to be
// null here
awindillman marked this conversation as resolved.
Show resolved Hide resolved
currentFold.setEndOffset(endOffs);
currentFold = currentFold.getParent();
foldStartLeadingWhiteSpaceCounts.pop();
Expand All @@ -106,6 +112,11 @@ else if (leadingWhiteSpaceCount > currentLeadingWhiteSpaceCount) {
return folds;
}

private static boolean tokenHasLineContinuation(Token t) {
t = t.getLastNonCommentNonWhitespaceToken();
return t!= null && t.isSingleChar('\\');
}

private static int getLeadingWhiteSpaceCount(Token t, int tabSize) {

// Lines continuing a multi-line string or char don't count
Expand All @@ -118,7 +129,7 @@ private static int getLeadingWhiteSpaceCount(Token t, int tabSize) {
int count = 0;
while (t != null && t.isPaintable()) {
if (!t.isWhitespace()) {
// Note Python doesn't nave multi-line comments so we don't
// Note Python doesn't nave multi-line comments, so we don't
// have to worry about MLD's
return t.getType() == TokenTypes.COMMENT_EOL ? -1 : count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,16 @@ import org.fife.ui.rsyntaxtextarea.TokenImpl;
}


@Override
public boolean getShouldIndentNextLineAfter(Token t) {
if (t!=null && t.length()==1) {
char ch = t.charAt(0);
return ch==':' || ch=='\\';
}
return false;
}


/**
* Returns the first token in the linked list of tokens generated
* from <code>text</code>. This method must be implemented by
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ void testGetFold_happyPath() {
"\n" +
"def Greg2JD(year, month, day):\n" +
"\n" +
" if (month < 3):\n" +
" if (month < 3) \\\\ \n" +
"and true:\n" +
" y = float(year) - 1.0\n" +
" m = float(month) + 12.0\n";
awindillman marked this conversation as resolved.
Show resolved Hide resolved

Expand Down