Skip to content

Modifies autoPair bracket clash around highlighted portions#95

Open
virinchiv wants to merge 1 commit intogracelang:autopairfrom
virinchiv:autopair
Open

Modifies autoPair bracket clash around highlighted portions#95
virinchiv wants to merge 1 commit intogracelang:autopairfrom
virinchiv:autopair

Conversation

@virinchiv
Copy link
Contributor

Updates the addCharEq function.

@apblack apblack self-requested a review April 16, 2024 04:22
Copy link
Contributor

@apblack apblack left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congratulations of figuring out how to solve this issue! Now that you have something working, I've made suggestions on how to shorten your code. I don't know if selection.start is always ≤ selection.end. If it is, then you don't need abs. If it is not, then I don't think that your code for multi-line selections will work

// Get the word length and corresponding text range
var wordHighlighted = isWordHighlighted(editor);
var wordlength = getHighlightedWordLength(editor);
var leftTextWord = editor.session.getTextRange(new Range(cursor.row, cursor.column - wordlength - 2, cursor.row, cursor.column - wordlength))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is quite a bit of code duplication between this if branch and the branch on line 650. I think that the code here is more general; if wordLength === 0, then won't this code accomplish exactly the same thing as the code on line 667?

Also, looking at your code for getHighlightedWordLength, it seems to me that it will return 0 when isWordHighlighted(...) returns false. So, I think that you can eliminate the duplication, and also eliminate the isWordHighlighted(...) function.

// Multi-line selection, count the number of characters in each line
var numLines = Math.abs(selectionRange.end.row - selectionRange.start.row) + 1;
var totalLength = 0;
for (var i = 0; i < numLines; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop should run from selectionRange.start.row to selectionRange.end.row. Actually, it would be better to make it run from selectionRange.start.row + 1 to ``selectionRange.end.row - 1`, because you deal with the first and last rows separately, so those cases should be moved outside the loop.

return Math.abs(selectionRange.end.column - selectionRange.start.column);
} else {
// Multi-line selection, count the number of characters in each line
var numLines = Math.abs(selectionRange.end.row - selectionRange.start.row) + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your use of abs hints that it is possible for start.row > end.row. In that case, I don't think that your loop will work — it uses i++ and always adds to start.row.

When the auto-pair bracket feature is turned on, typing [ inserts ].
Typing a second [ inserts a second ], and converts the [[ to ⟦, but
doesn't convert the ]] to ⟧, as it should.  This commit fixes that problem.
Moreover, when surrounding existing code with [ and ], the brackets
are converted to double brackets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants