Skip to content

Commit

Permalink
Add logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmet committed Mar 4, 2014
1 parent 8a26bdc commit d3d4e34
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 47 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
atom-ObjC2RubyMotion
atom-objc-2-rubymotion
====================

Objective-C to RubyMotion converter converted to use with atom
Objective-C to RubyMotion converter converted to use with atom editor
2 changes: 1 addition & 1 deletion keymaps/ascii-art.cson → keymaps/objc-2-rubymotion.cson
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@
# For more detailed documentation see
# https://www.atom.io/docs/latest/advanced/keymaps
'.editor':
'cmd-alt-a': 'ascii-art:convert'
'cmd-alt-a': 'rubymotion:convert'
14 changes: 0 additions & 14 deletions lib/ascii-art.coffee

This file was deleted.

110 changes: 110 additions & 0 deletions lib/objc-2-rubymotion.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
class Converter
constructor: (string) ->
@orig = string
@s = string


result: ->
@multilines_to_one_line()
@replace_nsstring()
@mark_spaces_in_string()
@convert_square_brackets_expression()
@remove_semicolon_at_the_end()
@remove_autorelease()
@remove_type_declaration()
@restore_spaces_in_string()

#allow idempotency
result = @s
@s = @orig

return result

# HELPERS

convert_args: (match, groups...) =>
# Consider args with colon followed by spaces
following_args = groups[1].replace /([^:]+)(\s+)(\S+):/g, '$1,$3:'

# Clear extra spaces after colons
following_args = following_args.replace /:\s+/g, ':'

return "#{groups[0]}(#{following_args})"

ruby_style_code: (match, groups...) =>
arg_pattern = /([^:]+)\:\s*(.+)/

msg = groups[1].replace(arg_pattern, @convert_args)
return "#{groups[0]}.#{msg}"

space_to_mark: (match, groups...) =>
return groups[0].replace /\s/g, '__SPACE__'

# CONVERSIONS

multilines_to_one_line: ->
# Remove trailing white space first. Refs: TrimTrailingWhiteSpace
@s = @s.replace /[\t ]+$/, ''
@s = @s.replace /([^;\s])$\n\s*/mg, '$1 '

return this

replace_nsstring: ->
@s = @s.replace /@("(?:[^\\"]|\\.)*")/g, '$1'

return this

mark_spaces_in_string: ->
@s = @s.replace /("(?:[^\\"]|\\.)*")/g, @space_to_mark

return this

restore_spaces_in_string: ->
@s = @s.replace /__SPACE__/g, ' '

return this


convert_square_brackets_expression: ->
max_attempt = 10 # Avoid infinite loops
attempt_count = 0
square_pattern = /\[([^\[\]]+?)\s+([^\[\]]+?)\]/g

loop
attempt_count += 1
if attempt_count > max_attempt
break
else if square_pattern.test @s
@s = @s.replace(square_pattern, @ruby_style_code)
else
break

return this

remove_semicolon_at_the_end: ->
@s = @s.replace /;/g, ''

return this

remove_autorelease: ->
@s = @s.replace /\.autorelease/g, ''

return this

remove_type_declaration: ->
@s = @s.replace /([^\s\S]*)[a-zA-Z_0-9]+\s*\*\s*([^=]+)=/gm, '$1$2='

return this

@Converter = Converter


module.exports =
activate: ->
atom.workspaceView.command "rubymotion:convert", => @convert()

convert: ->
# This assumes the active pane item is an editor
selection = atom.workspace.getActiveEditor().getSelection()
converter = new Converter selection.getText()
selection.insertText(converter.result())
15 changes: 6 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
{
"name": "ascii-art",
"main": "./lib/ascii-art",
"version": "0.0.0",
"private": true,
"name": "objc-2-rubymotion",
"main": "./lib/objc-2-rubymotion",
"version": "0.0.1",
"description": "A short description of your package",
"activationEvents": ["ascii-art:convert"],
"repository": "https://github.com/atom/ascii-art",
"activationEvents": ["rubymotion:convert"],
"repository": "https://github.com/ahmetabdi/atom-objc-2-rubymotion",
"license": "MIT",
"engines": {
"atom": ">0.39.0"
},
"dependencies": {
"figlet": "1.0.8"
}
"dependencies": {}
}
21 changes: 0 additions & 21 deletions spec/ascii-art-spec.coffee

This file was deleted.

0 comments on commit d3d4e34

Please sign in to comment.