Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add command to convert 1.8 hash syntax into new alternate (hashrocket-free) syntax #19

Merged
merged 2 commits into from

2 participants

@whatcould

To either annoy or placate other developers you're working with, depending on their syntax preference.

  • Converts current line or selected text
  • Uses option-command-= for shortcut key (shortcut for hash-rocket is control-L, but that seemed more ergonomic than memorable).
@whatcould whatcould Add command to convert hashrocket syntax to 1.9 hash syntax
* Uses option-command-equals for shortcut key
* Converts current line or selected text
96020fb
@JEG2

I think it's handy to have a command like this, but I think we should make it toggle between the old and new styles. This makes it fit better with commands like Ruby → Toggle String / Symbol and Source → Toggle *.

If we do make that change, it looks the the toggle commands usually use a keyboard shortcut of ⌃ plus some meaningful character. ⌃: and ⌃{ are taken, so maybe we could use your = but with ⌃ for ⌃=.

Thoughts?

@whatcould

Right, I thought of htat but then I'd have to figure out how to write a regex to toggle back from the new syntax to the old!

The hashrocket syntax is pretty straightforward, but the new syntax is a lot harder to reliably match, since colons are in regular use. Take this hash, for example:

{book: "A Book Title: A book subtitle", sentence: "Some may say: try harder"}

Guess I'd just have to ignore colons within strings? I'd have to think about that for a bit.

@JEG2

Yeah. You could have TextMate pass you the scope delimited content, but I'm not sure that's even needed in this case. This gets us reasonably close, I think:

>> %q{{book: "A Book Title: A book subtitle", sentence: 'Some may \'say: try harder\'', number: 1_024, var: var, method: m(1, 2, 3)}}.gsub(/\G(\{|,\s*)(\w+):(\s*(?:"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|\w+\([^)]*\)|[^,]+))/
=> "{:book => \"A Book Title: A book subtitle\", :sentence => 'Some may \\'say: try harder\\'', :number => 1_024, :var => var, :method => m(1, 2, 3)}"
@whatcould

Wow, that's impressive. I think you're missing some of the command? The second param to gsub is missing.

@JEG2

Sorry, I'm not sure how that got cut off:

gsub(/\G(\{|,\s*)(\w+):(\s*(?:"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|\w+\([^)]*\)|[^,]+))/, "\\1:\\2 =>\\3")
@whatcould

Ok, thanks, that is amazing. Works great. I changed it to toggle and switched the shortcut to ^=

I had to tweak it a bit -- for the toggles it's nice to have it do the whole line, and hashes don't necessarily start with { either. I suppose there might be a case where it doesn't work, but for every case in my current project it worked great, including Selecting all and hitting ^=.

@JEG2 JEG2 merged commit b6a4c65 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 10, 2011
  1. @whatcould

    Add command to convert hashrocket syntax to 1.9 hash syntax

    whatcould authored
    * Uses option-command-equals for shortcut key
    * Converts current line or selected text
Commits on Oct 11, 2011
  1. @whatcould
This page is out of date. Refresh to see the latest.
Showing with 30 additions and 0 deletions.
  1. +29 −0 Commands/Convert Ruby hash to 1_9 syntax.tmCommand
  2. +1 −0  info.plist
View
29 Commands/Convert Ruby hash to 1_9 syntax.tmCommand
@@ -0,0 +1,29 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
+<plist version="1.0">
+<dict>
+ <key>beforeRunningCommand</key>
+ <string>nop</string>
+ <key>command</key>
+ <string>#!/usr/bin/env ruby
+
+print case str = STDIN.read
+ when /=&gt;/; str.gsub(/:(\w+)[\s]+=&gt;[\s]+/, '\1: ')
+ when /(\w+):/; str.gsub(/(\w+):(\s*(?:"(?:\\"|[^"])*"|'(?:\\'|[^'])*'|\w+\([^)]*\)|[^,]+))/, ":\\1 =&gt;\\2")
+ else; str
+end
+</string>
+ <key>fallbackInput</key>
+ <string>line</string>
+ <key>input</key>
+ <string>selection</string>
+ <key>keyEquivalent</key>
+ <string>^=</string>
+ <key>name</key>
+ <string>Toggle Ruby Hash 1.8/1.9 syntax</string>
+ <key>output</key>
+ <string>replaceSelectedText</string>
+ <key>uuid</key>
+ <string>F4EEB2B6-07D8-402F-8FC3-79B7308D2576</string>
+</dict>
+</plist>
View
1  info.plist
@@ -433,6 +433,7 @@
<key>ordering</key>
<array>
<string>35222962-C50D-4D58-A6AE-71E7AD980BE4</string>
+ <string>F4EEB2B6-07D8-402F-8FC3-79B7308D2576</string>
<string>5289EE40-86B8-11D9-A8D4-000A95E13C98</string>
<string>63F3B3B7-CBE2-426B-B551-657733F3868B</string>
<string>1AD6A138-2E89-4D6A-AB3F-416BF9CE968D</string>
Something went wrong with that request. Please try again.