@@ -17,6 +17,11 @@ import {
17
17
} from '../../../out/cljs-lib/cljs-lib' ;
18
18
import * as util from '../../utilities' ;
19
19
import { isUndefined , cloneDeep } from 'lodash' ;
20
+ import { LispTokenCursor } from '../../cursor-doc/token-cursor' ;
21
+
22
+ const FormatDepthDefaults = {
23
+ deftype : 2 ,
24
+ } ;
20
25
21
26
export async function indentPosition ( position : vscode . Position , document : vscode . TextDocument ) {
22
27
const editor = util . getActiveTextEditor ( ) ;
@@ -67,7 +72,7 @@ export async function formatRangeEdits(
67
72
text ,
68
73
document . getText ( ) ,
69
74
rangeTuple ,
70
- document . eol == 2 ? '\r\n' : '\n'
75
+ _convertEolNumToStringNotation ( document . eol )
71
76
) ;
72
77
if ( newText ) {
73
78
return [ vscode . TextEdit . replace ( range , newText ) ] ;
@@ -94,20 +99,14 @@ export async function formatPositionInfo(
94
99
extraConfig = { }
95
100
) {
96
101
const doc : vscode . TextDocument = editor . document ;
97
- const pos : vscode . Position = editor . selection . active ;
98
- const index = doc . offsetAt ( pos ) ;
99
- const mirroredDoc : MirroredDocument = getDocument ( doc ) ;
100
- const cursor = mirroredDoc . getTokenCursor ( index ) ;
101
- const formatDepth = extraConfig [ 'format-depth' ] ? extraConfig [ 'format-depth' ] : 1 ;
102
- const isComment = cursor . getFunctionName ( ) === 'comment' ;
103
- const config = { ...extraConfig , 'comment-form?' : isComment } ;
104
- let formatRange = cursor . rangeForList ( formatDepth ) ;
105
- if ( ! formatRange ) {
106
- formatRange = cursor . rangeForCurrentForm ( index ) ;
107
- if ( ! formatRange || ! formatRange . includes ( index ) ) {
108
- return ;
109
- }
102
+ const index = doc . offsetAt ( editor . selection . active ) ;
103
+ const cursor = getDocument ( doc ) . getTokenCursor ( index ) ;
104
+
105
+ const formatRange = _calculateFormatRange ( extraConfig , cursor , index ) ;
106
+ if ( ! formatRange ?. includes ( index ) ) {
107
+ return ;
110
108
}
109
+
111
110
const formatted : {
112
111
'range-text' : string ;
113
112
range : number [ ] ;
@@ -116,9 +115,12 @@ export async function formatPositionInfo(
116
115
doc . getText ( ) ,
117
116
formatRange ,
118
117
index ,
119
- doc . eol == 2 ? '\r\n' : '\n' ,
118
+ _convertEolNumToStringNotation ( doc . eol ) ,
120
119
onType ,
121
- config
120
+ {
121
+ ...extraConfig ,
122
+ 'comment-form?' : cursor . getFunctionName ( ) === 'comment' ,
123
+ }
122
124
) ;
123
125
const range : vscode . Range = new vscode . Range (
124
126
doc . positionAt ( formatted . range [ 0 ] ) ,
@@ -135,6 +137,21 @@ export async function formatPositionInfo(
135
137
} ;
136
138
}
137
139
140
+ function _calculateFormatRange (
141
+ config : { 'format-depth' ?: number } ,
142
+ cursor : LispTokenCursor ,
143
+ index : number
144
+ ) {
145
+ const formatDepth = config ?. [ 'format-depth' ] ?? _formatDepth ( cursor ) ;
146
+ return cursor . rangeForList ( formatDepth ) ?? cursor . rangeForCurrentForm ( index ) ;
147
+ }
148
+
149
+ function _formatDepth ( cursor : LispTokenCursor ) {
150
+ const cursorClone = cursor . clone ( ) ;
151
+ cursorClone . backwardFunction ( 1 ) ;
152
+ return FormatDepthDefaults ?. [ cursorClone . getFunctionName ( ) ] ?? 1 ;
153
+ }
154
+
138
155
export async function formatPosition (
139
156
editor : vscode . TextEditor ,
140
157
onType : boolean = false ,
@@ -195,7 +212,7 @@ export function trimWhiteSpacePositionCommand(editor: vscode.TextEditor) {
195
212
export async function formatCode ( code : string , eol : number ) {
196
213
const d = {
197
214
'range-text' : code ,
198
- eol : eol == 2 ? '\r\n' : '\n' ,
215
+ eol : _convertEolNumToStringNotation ( eol ) ,
199
216
config : await config . getConfig ( ) ,
200
217
} ;
201
218
const result = jsify ( formatText ( d ) ) ;
@@ -249,3 +266,7 @@ async function _formatRange(
249
266
return result [ 'range-text' ] ;
250
267
}
251
268
}
269
+
270
+ function _convertEolNumToStringNotation ( eol : vscode . EndOfLine ) {
271
+ return eol == 2 ? '\r\n' : '\n' ;
272
+ }
0 commit comments