-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Added support for multi-cursor comments to the comment plugin #3543
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
Open
MiguelRoldao
wants to merge
9
commits into
zyedidia:master
Choose a base branch
from
MiguelRoldao:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
59ee5d9
add support for multi-cursor comments
MiguelRoldao 05a6870
added comment type for the FreeST language
MiguelRoldao f794609
added comment type for the FreeST language pt.2
MiguelRoldao f896faa
aesthetics
MiguelRoldao a023a00
removed freest
MiguelRoldao 0165e41
added exception for selections that end in a new line (for backwards …
MiguelRoldao 1c1a7d9
bugfixes
Andriamanitra 274e35d
Merge branch 'master'
fb2f136
fixed versioning and removed unused code
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| VERSION = "1.0.0" | ||
| VERSION = "1.1.0" | ||
|
|
||
| local util = import("micro/util") | ||
| local config = import("micro/config") | ||
|
|
@@ -78,66 +78,31 @@ end | |
| function isCommented(bp, lineN, commentRegex) | ||
| local line = bp.Buf:Line(lineN) | ||
| local regex = commentRegex:gsub("%s+", "%s*") | ||
| if string.match(line, regex) then | ||
| return true | ||
| end | ||
| return false | ||
| return string.match(line, regex) | ||
| end | ||
|
|
||
| function commentLine(bp, lineN, indentLen) | ||
| updateCommentType(bp.Buf) | ||
|
|
||
| local line = bp.Buf:Line(lineN) | ||
| local commentType = bp.Buf.Settings["commenttype"] | ||
| local sel = -bp.Cursor.CurSelection | ||
| local curpos = -bp.Cursor.Loc | ||
| local index = string.find(commentType, "%%s") - 1 | ||
| local indent = string.sub(line, 1, indentLen) | ||
| local trimmedLine = string.sub(line, indentLen + 1) | ||
| trimmedLine = trimmedLine:gsub("%%", "%%%%") | ||
| local indent = string.sub(line, 1, indentLen) | ||
| local commentType = bp.Buf.Settings["commenttype"] | ||
| local commentedLine = commentType:gsub("%%s", trimmedLine) | ||
| bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), indent .. commentedLine) | ||
| if bp.Cursor:HasSelection() then | ||
| bp.Cursor.CurSelection[1].Y = sel[1].Y | ||
| bp.Cursor.CurSelection[2].Y = sel[2].Y | ||
| bp.Cursor.CurSelection[1].X = sel[1].X | ||
| bp.Cursor.CurSelection[2].X = sel[2].X | ||
| else | ||
| bp.Cursor.X = curpos.X + index | ||
| bp.Cursor.Y = curpos.Y | ||
| end | ||
| bp.Cursor:Relocate() | ||
| bp.Cursor:StoreVisualX() | ||
| end | ||
|
|
||
| function uncommentLine(bp, lineN, commentRegex) | ||
| updateCommentType(bp.Buf) | ||
|
|
||
| local line = bp.Buf:Line(lineN) | ||
| local commentType = bp.Buf.Settings["commenttype"] | ||
| local sel = -bp.Cursor.CurSelection | ||
| local curpos = -bp.Cursor.Loc | ||
| local index = string.find(commentType, "%%s") - 1 | ||
| if not string.match(line, commentRegex) then | ||
| commentRegex = commentRegex:gsub("%s+", "%s*") | ||
| end | ||
| if string.match(line, commentRegex) then | ||
| uncommentedLine = string.match(line, commentRegex) | ||
| bp.Buf:Replace(buffer.Loc(0, lineN), buffer.Loc(#line, lineN), util.GetLeadingWhitespace(line) .. uncommentedLine) | ||
| if bp.Cursor:HasSelection() then | ||
| bp.Cursor.CurSelection[1].Y = sel[1].Y | ||
| bp.Cursor.CurSelection[2].Y = sel[2].Y | ||
| bp.Cursor.CurSelection[1].X = sel[1].X | ||
| bp.Cursor.CurSelection[2].X = sel[2].X | ||
| else | ||
| bp.Cursor.X = curpos.X - index | ||
| bp.Cursor.Y = curpos.Y | ||
| end | ||
| end | ||
| bp.Cursor:Relocate() | ||
| bp.Cursor:StoreVisualX() | ||
| end | ||
|
|
||
| -- unused | ||
| function toggleCommentLine(bp, lineN, commentRegex) | ||
| if isCommented(bp, lineN, commentRegex) then | ||
| uncommentLine(bp, lineN, commentRegex) | ||
|
|
@@ -146,9 +111,9 @@ function toggleCommentLine(bp, lineN, commentRegex) | |
| end | ||
| end | ||
|
|
||
| function toggleCommentSelection(bp, startLine, endLine, commentRegex) | ||
| function toggleCommentSelection(bp, lines, commentRegex) | ||
| local allComments = true | ||
| for line = startLine, endLine do | ||
| for line,_ in pairs(lines) do | ||
| if not isCommented(bp, line, commentRegex) then | ||
| allComments = false | ||
| break | ||
|
|
@@ -158,21 +123,22 @@ function toggleCommentSelection(bp, startLine, endLine, commentRegex) | |
| -- NOTE: we assume that the indentation is either tabs only or spaces only | ||
| local indentMin = -1 | ||
| if not allComments then | ||
| for line = startLine, endLine do | ||
| for line,_ in pairs(lines) do | ||
| local indentLen = #util.GetLeadingWhitespace(bp.Buf:Line(line)) | ||
| if indentMin == -1 or indentLen < indentMin then | ||
| indentMin = indentLen | ||
| end | ||
| end | ||
| end | ||
|
|
||
| for line = startLine, endLine do | ||
| for line,_ in pairs(lines) do | ||
| if allComments then | ||
| uncommentLine(bp, line, commentRegex) | ||
| else | ||
| commentLine(bp, line, indentMin) | ||
| end | ||
| end | ||
| return not allComments | ||
| end | ||
|
|
||
| function comment(bp, args) | ||
|
|
@@ -181,22 +147,43 @@ function comment(bp, args) | |
| local commentType = bp.Buf.Settings["commenttype"] | ||
| local commentRegex = "^%s*" .. commentType:gsub("%%","%%%%"):gsub("%$","%$"):gsub("%)","%)"):gsub("%(","%("):gsub("%?","%?"):gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%%%s", "(.*)") | ||
|
|
||
| if bp.Cursor:HasSelection() then | ||
| if bp.Cursor.CurSelection[1]:GreaterThan(-bp.Cursor.CurSelection[2]) then | ||
| local endLine = bp.Cursor.CurSelection[1].Y | ||
| if bp.Cursor.CurSelection[1].X == 0 then | ||
| endLine = endLine - 1 | ||
| local lines = {} | ||
| local curData = {} | ||
| -- gather cursor data and lines to (un)comment | ||
| for i = 0,#bp.Buf:getCursors()-1 do | ||
| local cursor = bp.Buf:getCursor(i) | ||
|
||
| local hasSelection = cursor:HasSelection() | ||
| table.insert(curData, { | ||
| sel = -cursor.CurSelection, | ||
| curpos = -cursor.Loc, | ||
| cursor = cursor, | ||
| hasSelection = hasSelection | ||
| }) | ||
| if hasSelection then | ||
| for lineN = cursor.CurSelection[1].Y, cursor.CurSelection[2].Y do | ||
| lines[lineN] = true | ||
| end | ||
| toggleCommentSelection(bp, bp.Cursor.CurSelection[2].Y, endLine, commentRegex) | ||
| else | ||
| local endLine = bp.Cursor.CurSelection[2].Y | ||
| if bp.Cursor.CurSelection[2].X == 0 then | ||
| endLine = endLine - 1 | ||
| end | ||
| toggleCommentSelection(bp, bp.Cursor.CurSelection[1].Y, endLine, commentRegex) | ||
| lines[cursor.Y] = true | ||
| end | ||
| else | ||
| toggleCommentLine(bp, bp.Cursor.Y, commentRegex) | ||
| end | ||
| -- (un)comment selected lines | ||
| local commented = toggleCommentSelection(bp, lines, commentRegex) | ||
| -- restore cursors | ||
| local displacement = (string.find(commentType, "%%s") - 1) * (commented and 1 or -1) | ||
| for i=1,#curData do | ||
| local cursor = curData[i].cursor | ||
| if curData[i].hasSelection then | ||
| cursor.CurSelection[1].Y = curData[i].sel[1].Y | ||
| cursor.CurSelection[2].Y = curData[i].sel[2].Y | ||
| cursor.CurSelection[1].X = curData[i].sel[1].X + displacement | ||
| cursor.CurSelection[2].X = curData[i].sel[2].X + displacement | ||
| else | ||
| cursor.Y = curData[i].curpos.Y | ||
| cursor.X = curData[i].curpos.X + displacement | ||
| end | ||
| cursor:Relocate() | ||
| cursor:StoreVisualX() | ||
| end | ||
| end | ||
|
|
||
|
|
@@ -210,3 +197,5 @@ function init() | |
| config.TryBindKey("CtrlUnderscore", "lua:comment.comment", false) | ||
| config.AddRuntimeFile("comment", config.RTHelp, "help/comment.md") | ||
| end | ||
|
|
||
| -- So if im just writting, what finger do i use th e most to input spaces. Funnily engouh, I mostly use the right hand do do that, and not my left hand at all. That's quite insteresting. Altough iut must be said, I've been looking at how my hands moving while typing, and ids anything but efficient :( But oh well. it only goes to show how ineficient typing on a regular key board can be :/ | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.