Skip to content

Instantly share code, notes, and snippets.

@lrhn
Created July 16, 2018 06:27
Show Gist options
  • Save lrhn/0b58ffd0613edcb59ddaae26933c217e to your computer and use it in GitHub Desktop.
Save lrhn/0b58ffd0613edcb59ddaae26933c217e to your computer and use it in GitHub Desktop.
Sublime Plugin: .codeUnitAt(0)
import sublime
import sublime_plugin
import re
class DartCodeUnitCommand(sublime_plugin.TextCommand):
def run(self, edit):
rd = re.compile(r'r?([\'"])(.)\1\.codeUnitAt\(0\)');
for selection in self.view.sel():
selectionText = self.view.substr(selection);
matches = list(rd.finditer(selectionText))
if (len(matches) > 0):
for match in reversed(matches):
matchText = match.group(0)
originalLength = len(matchText)
replacement = str(hex(ord(match.group(2)))) + " /*'" + match.group(2) + "'*/"
selectionText = (selectionText[:match.start(0)] +
replacement +
selectionText[match.end(0):])
self.view.replace(edit, selection, selectionText)
else:
self.view.replace(edit, selection, selectionText + "/*NO*/");
# Converts: 'x'.codeUnitAt(0)
# To: 0x58 /*'x'*/
# Works with any single-character single-line string.
#
# Add to User keymap:
# { "keys": ["shift+ctrl+c"], "command": "dart_code_unit" },
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment