Latest version of autotag
Brandon [08-30-17 - 09:08]
Latest version of autotag
Multiple elements per line are now supported. Self closing tags will not trigger the plugin. Make sure that you don't try and skip back a few tags to enter a tag you might have forgotten, in that case you'll have to type in your own closing tags.
If you're opening a tag that already has a closing tag in the document and you want more than one of that particular tag just enter an extra chevron ">" to trigger the autocomplete
diff --git a/autotag3.lua b/autotag3.lua
new file mode 100644
index 0000000..165d955
--- /dev/null
+++ b/autotag3.lua
@@ -0,0 +1,33 @@
+local tagtoclose
+return {
+ name = "Autotag for xml",
+ description = "Auto-enters closing tags for xml",
+ author = "Brandon Wall @Solvaring",
+ version = 0.1,
+ onEditorCharAdded = function(self, editor, event) --On Character Added editor event handler [Event Handlers Listed Here](https://github.com/pkulchenko/ZeroBraneStudio/blob/master/packages/sample.lua)
+ local tag = editor:GetCurLine() -- Retrieve text of the line containing caret, store in `tag`
+ local curkey = event:GetKey() -- Get value of current key being pressed and assign to `curkey`
+ if curkey == 62 and tag:find("%b<>") then -- If current key `curkey` is ">" and current line `tag` contains a balanced string resembling an html/xml tag
+ local curpos = editor:GetCurrentPos() --Get Current position of caret and store in curpos
+ for stag in tag:gmatch("<([%a%d]+)>") do -- Get the last opened tag on current line and store in `tagtoclose`
+ tagtoclose = stag
+ end
+ if tagtoclose:find([[/>]]) then return end -- if the tag is self-closing then return
+ local document = editor:GetText() -- Store all text currently in editor
+ local fullclosingtag = "</"..tagtoclose..">" -- build closing tag
+ local fullopeningtag = "<"..tagtoclose..">" -- build opening tag
+ if document:find(fullopeningtag..">") then -- if opening tag with extra chevron found
+ editor:AddText(fullclosingtag) -- add another closing tag
+ editor:SetEmptySelection(curpos) -- reset caret
+ editor:DeleteRange(curpos-1, 1) -- delete extra chevron ">"
+ return
+ elseif document:find(fullclosingtag) then -- If closing tag already found in document then return. The If part of this block above circumvents this behavior as a sort of override switch to enter an nth duplicate closing tag
+ return
+ end
+ editor:AddText(fullclosingtag) -- add closing tag
+ editor:SetEmptySelection(curpos) -- Reset cursor position so entries can begin to be made inbetween the opening and closing tag.
+ ::breakout::
+ end
+ end
+
+}
\ No newline at end of file