Skip to content

Commit

Permalink
Enhanced function for line joining
Browse files Browse the repository at this point in the history
  • Loading branch information
echuraev committed Nov 9, 2018
1 parent 361406a commit f53d734
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
13 changes: 10 additions & 3 deletions autoload/common/common.vim
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,16 @@ function! common#common#GetVisualSelection() abort
endfunction

function! common#common#JoinLinesInText(text)
let text = substitute(a:text, " ", " ", "g")
let text = substitute(text, "\n", " ", "g")
let text = substitute(text, " ", "\\n", "g")
let text = substitute(a:text, '\t', ' ', 'g')
let lines = split(text, '\n')
let final_lines = []
for line in lines
let line = substitute(line, '\v^[ ]*', '', 'g') " Remove spaces in the beginning of the line
let line = substitute(line, '\v[ ]{2,}', ' ', 'g') " Remove more than one space in one place
call add(final_lines, line)
endfor
let text = join(final_lines, ' ')
let text = substitute(text, ' ', '\n', 'g')
return text
endfunction

Expand Down
6 changes: 5 additions & 1 deletion doc/trans.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
Author: Egor Churaev <[email protected]>
Licence: GPLv3
Homepage: https://github.com/echuraev/translate-shell.vim
Version: 2.0
Version: 2.0.1

==============================================================================
CONTENTS *trans* *translate-shell.vim* *trans-context*
Expand Down Expand Up @@ -561,6 +561,10 @@ Example:
==============================================================================
CHANGELOG *trans-changelog*

2.0.1 (2018-11-09)
- Added search of translate direction in passed args
- Enhanced function for line joining

2.0 (2018-11-03)
- Unified :Trans and :TransVisual functions

Expand Down
21 changes: 21 additions & 0 deletions test/check-common-functions.vader
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,27 @@ Expect text:
Hello!
I would like to see how lines will joined!!!

Execute (common#common#JoinLinesInText: Check join lines with spaces in the beginning):
let text = "Header!\n\nList to join:\n * item1\n * Long item 2\n * Multiline item\n with text\n\t* Mutliple item\n\t\ttext with tab"
silent! put = text
silent! put = ''
silent! put = common#common#JoinLinesInText(text)

Expect text:

Header!

List to join:
* item1
* Long item 2
* Multiline item
with text
* Mutliple item
text with tab

Header!
List to join: * item1 * Long item 2 * Multiline item with text * Mutliple item text with tab

Execute (common#common#GenerateInputlist: Check generation of inputlist):
let text = "Select item:"
let list = ['item 1', 'item 2', 'item 3']
Expand Down

0 comments on commit f53d734

Please sign in to comment.