-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* unpack: Documentation Reverse an :Unpack with :Inline Repeat.vim support for :Unpack Initial work on :Unpack
- Loading branch information
Showing
5 changed files
with
385 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
function! ember_tools#unpack#Run() | ||
" TODO (2016-08-07) Multiline imports (look for the closing ; of the line?) | ||
" TODO (2016-07-06) Nested unpacking: | ||
" const { computed, Controller, inject: { service }, observer } = Ember; | ||
|
||
let saved_view = winsaveview() | ||
|
||
if !search('\%(\k\|\.\)\+', 'bc', line('.')) | ||
return | ||
endif | ||
|
||
let namespace = expand('<cword>') | ||
normal! "_df. | ||
let member = expand('<cword>') | ||
|
||
" Look for an existing unpacking | ||
if search('const {.*'.member.'.*}\s\+=\s\+'.namespace, 'n') | ||
" this member of the namespace is already unpacked, nothing to do | ||
return | ||
endif | ||
|
||
if search('const {.*}\s\+=\s\+'.namespace) | ||
" we found an existing unpacking without this member, unpack it here | ||
let unpacking = getline('.') | ||
let unpacking = substitute(unpacking, | ||
\ '\s*}\(\s\+=\s\+'.namespace.'\)', | ||
\ ', '.member.' } = '.namespace, | ||
\ 'g') | ||
call setline('.', unpacking) | ||
|
||
call winrestview(saved_view) | ||
silent! call repeat#set(":call ember_tools#unpack#Run(0)\<cr>") | ||
return | ||
endif | ||
|
||
" if we're here, there's no existing unpacking | ||
if search('^const {', 'bW') | ||
" we can add it after the last unpacking | ||
call append(line('.'), ['']) | ||
normal! j | ||
elseif search('^import', 'bW') | ||
" we can add it after the last import | ||
call append(line('.'), ['', '']) | ||
normal! jj | ||
else | ||
" just add it at the top of the file | ||
call append(0, ['', '']) | ||
normal! gg | ||
endif | ||
|
||
call setline('.', 'const { '.member.' } = '.namespace.';') | ||
call winrestview(saved_view) | ||
silent! call repeat#set(":call ember_tools#unpack#Run()\<cr>") | ||
endfunction | ||
|
||
function! ember_tools#unpack#Reverse() | ||
let saved_view = winsaveview() | ||
let variable = expand('<cword>') | ||
|
||
if searchpair('const {', '', '}\s*=\s*\zs\k\+', 'W') <= 0 | ||
return | ||
endif | ||
|
||
let prefix = expand('<cword>') | ||
|
||
call search('\<'.variable.'\>', 'bW') | ||
|
||
" Remove variable from const line | ||
exe 's/,\s*\%#'.variable.'//e' | ||
exe 's/\%#'.variable.',\=\s*\ze\%(\k\| }\)//e' | ||
|
||
" Handle empty const blocks | ||
if getline('.') =~ '^const {\s*} =' | ||
let next_lineno = nextnonblank(line('.') + 1) | ||
|
||
if getline(next_lineno) !~ '^const' | ||
" it's something other than another const line, let's delete all the | ||
" whitespace up until that point | ||
exe line('.').','.(next_lineno - 1).'delete _' | ||
else | ||
" just delete this line | ||
delete _ | ||
endif | ||
endif | ||
|
||
" Add prefix everywhere | ||
normal! G$ | ||
let search_flags = "w" | ||
let variable_pattern = '\%('.prefix.'\.\)\@<!\<'.variable.'\>' | ||
|
||
while search(variable_pattern, search_flags) > 0 | ||
if synIDattr(synID(line('.'), col('.'), 1), 'name') !~ 'String\|Comment' | ||
exe 'normal! i'.prefix.'.' | ||
" go back to the search | ||
call search(variable_pattern) | ||
endif | ||
let search_flags = "W" | ||
endwhile | ||
|
||
call winrestview(saved_view) | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
require 'spec_helper' | ||
|
||
describe ":Inline" do | ||
describe "const unpacking" do | ||
specify "deletes a const line if nothing is left of it after inlining" do | ||
edit_file 'test.js', <<-EOF | ||
const { Controller } = Ember; | ||
export default Controller.extend({}); | ||
EOF | ||
|
||
vim.search 'const { \zsController' | ||
vim.command 'Inline' | ||
vim.write | ||
|
||
expect_file_contents current_file, <<-EOF | ||
export default Ember.Controller.extend({}); | ||
EOF | ||
end | ||
|
||
specify "deletes an const line with another const line following" do | ||
edit_file 'test.js', <<-EOF | ||
const { Controller } = Ember; | ||
const { foo } = bar; | ||
export default Controller.extend({}); | ||
EOF | ||
|
||
vim.search 'const { \zsController' | ||
vim.command 'Inline' | ||
vim.write | ||
|
||
expect_file_contents current_file, <<-EOF | ||
const { foo } = bar; | ||
export default Ember.Controller.extend({}); | ||
EOF | ||
end | ||
|
||
specify "inlines entries from the beginning" do | ||
edit_file 'test.js', <<-EOF | ||
import Ember from 'ember'; | ||
const { computed, Controller, isPresent } = Ember; | ||
export default Controller.extend({ | ||
foo: Ember.computed.equal('bar', 'baz') | ||
}); | ||
EOF | ||
|
||
vim.search 'const.*\zscomputed' | ||
vim.command 'Inline' | ||
vim.write | ||
|
||
expect_file_contents current_file, <<-EOF | ||
import Ember from 'ember'; | ||
const { Controller, isPresent } = Ember; | ||
export default Controller.extend({ | ||
foo: Ember.computed.equal('bar', 'baz') | ||
}); | ||
EOF | ||
end | ||
|
||
specify "inlines entries in the middle" do | ||
edit_file 'test.js', <<-EOF | ||
import Ember from 'ember'; | ||
const { computed, Controller, isPresent } = Ember; | ||
export default Controller.extend({ | ||
foo: Ember.computed.equal('bar', 'baz') | ||
}); | ||
EOF | ||
|
||
vim.search 'const.*\zsController' | ||
vim.command 'Inline' | ||
vim.write | ||
|
||
expect_file_contents current_file, <<-EOF | ||
import Ember from 'ember'; | ||
const { computed, isPresent } = Ember; | ||
export default Ember.Controller.extend({ | ||
foo: Ember.computed.equal('bar', 'baz') | ||
}); | ||
EOF | ||
end | ||
|
||
specify "inlines entries at the end" do | ||
edit_file 'test.js', <<-EOF | ||
import Ember from 'ember'; | ||
const { computed, Controller, isPresent } = Ember; | ||
export default Controller.extend({ | ||
foo: Ember.computed.equal('bar', 'baz') | ||
}); | ||
EOF | ||
|
||
vim.search 'const.*\zsisPresent' | ||
vim.command 'Inline' | ||
vim.write | ||
|
||
expect_file_contents current_file, <<-EOF | ||
import Ember from 'ember'; | ||
const { computed, Controller } = Ember; | ||
export default Controller.extend({ | ||
foo: Ember.computed.equal('bar', 'baz') | ||
}); | ||
EOF | ||
end | ||
end | ||
end |
Oops, something went wrong.