├── README.md ├── comment.lua ├── help └── comment-plugin.md └── repo.json /README.md: -------------------------------------------------------------------------------- 1 | # Comment Plugin for Micro 2 | 3 | **Deprecated** This plugin is now provided by default with micro and the version here should not be used. 4 | 5 | This plugin provides automatic commenting/uncommenting for micro. 6 | 7 | Install with `> plugin install comment`. 8 | 9 | See the [docs](https://github.com/micro-editor/comment-plugin/blob/master/help/comment-plugin.md) for more details. 10 | -------------------------------------------------------------------------------- /comment.lua: -------------------------------------------------------------------------------- 1 | VERSION = "1.0.6" 2 | 3 | ft = {} 4 | 5 | ft["c"] = "// %s" 6 | ft["c++"] = "// %s" 7 | ft["go"] = "// %s" 8 | ft["python"] = "# %s" 9 | ft["python3"] = "# %s" 10 | ft["html"] = "" 11 | ft["java"] = "// %s" 12 | ft["julia"] = "# %s" 13 | ft["perl"] = "# %s" 14 | ft["php"] = "// %s" 15 | ft["rust"] = "// %s" 16 | ft["shell"] = "# %s" 17 | ft["lua"] = "-- %s" 18 | ft["javascript"] = "// %s" 19 | ft["ruby"] = "# %s" 20 | ft["d"] = "// %s" 21 | ft["swift"] = "// %s" 22 | ft["julia"] = "# %s" 23 | ft["latex"] = "\% %s" 24 | 25 | function onViewOpen(v) 26 | if v.Buf.Settings["commenttype"] == nil then 27 | if ft[v.Buf.Settings["filetype"]] ~= nil then 28 | v.Buf.Settings["commenttype"] = ft[v.Buf.Settings["filetype"]] 29 | else 30 | v.Buf.Settings["commenttype"] = "# %s" 31 | end 32 | end 33 | end 34 | 35 | function commentLine(lineN) 36 | local v = CurView() 37 | local line = v.Buf:Line(lineN) 38 | local commentType = v.Buf.Settings["commenttype"] 39 | local commentRegex = "^%s*" .. commentType:gsub("%*", "%*"):gsub("%-", "%-"):gsub("%.", "%."):gsub("%+", "%+"):gsub("%]", "%]"):gsub("%[", "%["):gsub("%%s", "(.*)") 40 | local sel = -v.Buf.Cursor.CurSelection 41 | local curpos = -v.Buf.Cursor.Loc 42 | local index = string.find(commentType, "%%s") - 1 43 | if string.match(line, commentRegex) then 44 | uncommentedLine = string.match(line, commentRegex) 45 | v.Buf:Replace(Loc(0, lineN), Loc(#line, lineN), GetLeadingWhitespace(line) .. uncommentedLine) 46 | if v.Buf.Cursor:HasSelection() then 47 | v.Buf.Cursor.CurSelection[1].Y = sel[1].Y 48 | v.Buf.Cursor.CurSelection[2].Y = sel[2].Y 49 | v.Buf.Cursor.CurSelection[1].X = sel[1].X 50 | v.Buf.Cursor.CurSelection[2].X = sel[2].X 51 | else 52 | v.Buf.Cursor.X = curpos.X - index 53 | v.Buf.Cursor.Y = curpos.Y 54 | end 55 | else 56 | local commentedLine = commentType:gsub("%%s", trim(line)) 57 | v.Buf:Replace(Loc(0, lineN), Loc(#line, lineN), GetLeadingWhitespace(line) .. commentedLine) 58 | if v.Buf.Cursor:HasSelection() then 59 | v.Buf.Cursor.CurSelection[1].Y = sel[1].Y 60 | v.Buf.Cursor.CurSelection[2].Y = sel[2].Y 61 | v.Buf.Cursor.CurSelection[1].X = sel[1].X 62 | v.Buf.Cursor.CurSelection[2].X = sel[2].X 63 | else 64 | v.Buf.Cursor.X = curpos.X + index 65 | v.Buf.Cursor.Y = curpos.Y 66 | end 67 | end 68 | v.Cursor:Relocate() 69 | v.Cursor.LastVisualX = v.Cursor:GetVisualX() 70 | end 71 | 72 | function commentSelection(startLine, endLine) 73 | for line = startLine, endLine do 74 | commentLine(line) 75 | end 76 | end 77 | 78 | function comment() 79 | local v = CurView() 80 | if v.Cursor:HasSelection() then 81 | if v.Cursor.CurSelection[1]:GreaterThan(-v.Cursor.CurSelection[2]) then 82 | local endLine = v.Cursor.CurSelection[1].Y 83 | if v.Cursor.CurSelection[1].X == 0 then 84 | endLine = endLine - 1 85 | end 86 | commentSelection(v.Cursor.CurSelection[2].Y, endLine) 87 | else 88 | local endLine = v.Cursor.CurSelection[2].Y 89 | if v.Cursor.CurSelection[2].X == 0 then 90 | endLine = endLine - 1 91 | end 92 | commentSelection(v.Cursor.CurSelection[1].Y, endLine) 93 | end 94 | else 95 | commentLine(v.Cursor.Y) 96 | end 97 | end 98 | 99 | function trim(s) 100 | return (s:gsub("^%s*(.-)%s*$", "%1")) 101 | end 102 | 103 | function string.starts(String,Start) 104 | return string.sub(String,1,string.len(Start))==Start 105 | end 106 | 107 | MakeCommand("comment", "comment.comment") 108 | BindKey("Alt-/", "comment.comment") 109 | 110 | AddRuntimeFile("comment", "help", "help/comment-plugin.md") 111 | -------------------------------------------------------------------------------- /help/comment-plugin.md: -------------------------------------------------------------------------------- 1 | # Comment Plugin 2 | 3 | The comment plugin provides auto commenting/uncommenting. 4 | The default binding to comment/uncomment a line is `Alt-/`, 5 | but you can easily modify that in your `bindings.json` file: 6 | 7 | ```json 8 | { 9 | "Alt-g": "comment.comment" 10 | } 11 | ``` 12 | 13 | You can also execute a command which will do the same thing as 14 | the binding: 15 | 16 | ``` 17 | > comment 18 | ``` 19 | 20 | If you have a selection, the plugin will comment all the lines 21 | selected. 22 | 23 | The comment type will be auto detected based on the filetype, 24 | but it is only available for certain filetypes: 25 | 26 | * c: `// %s` 27 | * c++: `// %s` 28 | * d: `// %s` 29 | * go: `// %s` 30 | * html: `` 31 | * java: `// %s` 32 | * javascript: `// %s` 33 | * julia: `# %s` 34 | * lua: `-- %s` 35 | * perl: `# %s` 36 | * php: `// %s` 37 | * python: `# %s` 38 | * python3: `# %s` 39 | * ruby: `# %s` 40 | * rust: `// %s` 41 | * shell: `# %s` 42 | * swift: `// %s` 43 | 44 | If your filetype is not available here, you can simply modify 45 | the `commenttype` option: 46 | 47 | ``` 48 | set commenttype "/* %s */" 49 | ``` 50 | 51 | Or in your `settings.json`: 52 | 53 | ```json 54 | { 55 | "*.c": { 56 | "commenttype": "/* %s */" 57 | } 58 | } 59 | ``` 60 | -------------------------------------------------------------------------------- /repo.json: -------------------------------------------------------------------------------- 1 | [{ 2 | "Name": "comment", 3 | "Description": "Plugin to auto comment or uncomment lines", 4 | "Tags": ["comment", "uncomment"], 5 | "Versions": [ 6 | { 7 | "Version": "1.0.6", 8 | "Url": "https://github.com/micro-editor/comment-plugin/archive/v1.0.6.zip", 9 | "Require": { 10 | "micro": ">=1.1.0 <2.0.0" 11 | } 12 | } 13 | ] 14 | }] 15 | --------------------------------------------------------------------------------