1
- ace . define ( "ace/ext/hardwrap" , [ "require" , "exports" , "module" , "ace/range" ] , function ( require , exports , module ) {
1
+ ace . define ( "ace/ext/hardwrap" , [ "require" , "exports" , "module" , "ace/range" , "ace/editor" , "ace/config" ] , function ( require , exports , module ) {
2
2
"use strict" ;
3
3
4
4
var Range = require ( "../range" ) . Range ;
5
5
6
6
function hardWrap ( editor , options ) {
7
7
var max = options . column || editor . getOption ( "printMarginColumn" ) ;
8
+ var allowMerge = options . allowMerge != false ;
8
9
9
10
var row = Math . min ( options . startRow , options . endRow ) ;
10
11
var endRow = Math . max ( options . startRow , options . endRow ) ;
@@ -16,10 +17,11 @@ function hardWrap(editor, options) {
16
17
if ( line . length > max ) {
17
18
var space = findSpace ( line , max , 5 ) ;
18
19
if ( space ) {
19
- session . replace ( new Range ( row , space . start , row , space . end ) , "\n" ) ;
20
+ var indentation = / ^ \s * / . exec ( line ) [ 0 ] ;
21
+ session . replace ( new Range ( row , space . start , row , space . end ) , "\n" + indentation ) ;
20
22
}
21
23
endRow ++ ;
22
- } else if ( / \S / . test ( line ) && row != endRow ) {
24
+ } else if ( allowMerge && / \S / . test ( line ) && row != endRow ) {
23
25
var nextLine = session . getLine ( row + 1 ) ;
24
26
if ( nextLine && / \S / . test ( nextLine ) ) {
25
27
var trimmedLine = line . replace ( / \s + $ / , "" ) ;
@@ -32,6 +34,8 @@ function hardWrap(editor, options) {
32
34
session . replace ( replaceRange , " " ) ;
33
35
row -- ;
34
36
endRow -- ;
37
+ } else if ( trimmedLine . length < line . length ) {
38
+ session . remove ( new Range ( row , trimmedLine . length , row , line . length ) ) ;
35
39
}
36
40
}
37
41
}
@@ -65,14 +69,50 @@ function hardWrap(editor, options) {
65
69
if ( spaceBefore && spaceBefore [ 2 ] && spaceBefore . index > min ) {
66
70
return {
67
71
start : spaceBefore . index ,
68
- end : spaceBefore . index + spaceBefore [ 3 ] . length
72
+ end : spaceBefore . index + spaceBefore [ 2 ] . length
73
+ } ;
74
+ }
75
+ if ( spaceAfter && spaceAfter [ 2 ] ) {
76
+ start = max + spaceAfter [ 2 ] . length ;
77
+ return {
78
+ start : start ,
79
+ end : start + spaceAfter [ 3 ] . length
69
80
} ;
70
81
}
71
-
72
82
}
73
83
74
84
}
75
85
86
+ function wrapAfterInput ( e ) {
87
+ if ( e . command . name == "insertstring" && / \S / . test ( e . args ) ) {
88
+ var editor = e . editor ;
89
+ var cursor = editor . selection . cursor ;
90
+ if ( cursor . column <= editor . renderer . $printMarginColumn ) return ;
91
+ var lastDelta = editor . session . $undoManager . $lastDelta ;
92
+
93
+ hardWrap ( editor , {
94
+ startRow : cursor . row , endRow : cursor . row ,
95
+ allowMerge : false
96
+ } ) ;
97
+ if ( lastDelta != editor . session . $undoManager . $lastDelta )
98
+ editor . session . markUndoGroup ( ) ;
99
+ }
100
+ }
101
+
102
+ var Editor = require ( "../editor" ) . Editor ;
103
+ require ( "../config" ) . defineOptions ( Editor . prototype , "editor" , {
104
+ hardWrap : {
105
+ set : function ( val ) {
106
+ if ( val ) {
107
+ this . commands . on ( "afterExec" , wrapAfterInput ) ;
108
+ } else {
109
+ this . commands . off ( "afterExec" , wrapAfterInput ) ;
110
+ }
111
+ } ,
112
+ value : false
113
+ }
114
+ } ) ;
115
+
76
116
exports . hardWrap = hardWrap ;
77
117
78
118
} ) ; ( function ( ) {
0 commit comments