77} from 'vscode-languageserver'
88
99import * as stream from 'stream'
10+ import * as yaml from 'js-yaml'
1011import { ChildProcess } from "child_process" ;
12+ import { editorSettings } from './server' ;
1113
1214let skProtocolProcess : ChildProcess | null = null
1315let skeHandler : SourcekiteResponseHandler | null = null
@@ -144,16 +146,16 @@ let reqCount = 0 //FIXME
144146
145147type RequestType = "codecomplete" | "cursorinfo" | "demangle" | "editor.open" | "editor.formattext"
146148
147- function parseSkResponse ( resp : string ) : any {
148- return JSON . parse ( jsonify ( resp ) )
149- }
150-
151149function pluck < T , K extends keyof T > ( prop : K ) : ( ofTarget : T ) => T [ K ] {
152150 return target => target [ prop ] ;
153151}
154152
155- function typedResponse ( request : string , requestType : RequestType ,
156- extraState : any = null , retries = 0 ) : Promise < string > {
153+ function typedResponse < T > ( request : string , requestType : RequestType ,
154+ extraState : any = null , retries = 0 ) : Promise < T > {
155+ function parseSkResponse ( resp : string ) : any {
156+ return yaml . safeLoad ( resp ) ;
157+ }
158+
157159 server . trace ( 'to write request: ' , request )
158160 const rid = reqCount ++
159161 skProtocolProcess . stdin . write ( rid + "\n" )
@@ -168,7 +170,7 @@ function typedResponse(request: string, requestType: RequestType,
168170 restartSourcekite ( ) ;
169171 return typedResponse ( request , requestType , extraState , retries ) ;
170172 }
171- } )
173+ } ) . then ( parseSkResponse )
172174}
173175
174176function request (
@@ -225,13 +227,12 @@ function request(
225227//== codeComplete
226228export function codeComplete ( srcText : string , srcPath : string , offset : number ) : Promise < any > {
227229 return request ( "codecomplete" , srcText , srcPath , offset )
228- . then ( parseSkResponse ) . then ( pluck ( "key.results" ) ) ;
230+ . then ( pluck ( "key.results" ) ) ;
229231}
230232
231233//== cursorInfo
232234export function cursorInfo ( srcText : string , srcPath : string , offset : number ) : Promise < any > {
233- return request ( "cursorinfo" , srcText , srcPath , offset )
234- . then ( parseSkResponse ) ;
235+ return request ( "cursorinfo" , srcText , srcPath , offset ) ;
235236}
236237
237238//== demangle
@@ -243,8 +244,8 @@ export function demangle(...demangledNames: string[]): Promise<any> {
243244}
244245
245246`
246- return typedResponse ( request , "demangle" )
247- . then ( parseSkResponse ) . then ( pluck ( "key.results" ) )
247+ return typedResponse < any > ( request , "demangle" )
248+ . then ( pluck ( "key.results" ) )
248249}
249250
250251
@@ -260,7 +261,7 @@ export function editorFormatText(
260261 editorOpen ( srcPath , srcText , false , false , true )
261262 . then ( ( v ) => {
262263 // discard v
263- let p = requestEditorFormatText ( srcPath , lineStart , 1 , document )
264+ let p = requestEditorFormatText ( { file : srcPath , line : lineStart , document } )
264265
265266 //TODO async-await
266267 function nextp ( fts : FormatTextState ) {
@@ -269,7 +270,7 @@ export function editorFormatText(
269270 let sPos : Position = { line : fts . line , character : 0 }
270271 let ePos : Position = document . positionAt (
271272 document . offsetAt ( { line : fts . line + 1 , character : 0 } ) - 1 )
272- requestEditorFormatText ( srcPath , fts . line + 1 , 1 , document )
273+ requestEditorFormatText ( { file : srcPath , line : fts . line + 1 , document } )
273274 . then ( nextp )
274275 . catch ( ( err ) => {
275276 reject ( err )
@@ -307,52 +308,61 @@ function editorOpen(
307308}
308309
309310`
310- return typedResponse ( request , "editor.open" )
311- . then ( parseSkResponse )
311+ return typedResponse ( request , "editor.open" ) ;
312312}
313313
314314interface FormatTextState {
315315 line : number ,
316316 textEdit : TextEdit
317317}
318318
319- function requestEditorFormatText (
320- keyName : string ,
321- keyLine : number ,
322- keyLength : number , //FIXME unuseful now
319+ interface SkFormatTextResponse {
320+ 'key.line' : number ;
321+ 'key.length' : number ;
322+ 'key.sourcetext' : string ;
323+ }
324+
325+ function requestEditorFormatText ( options : {
326+ file : string ,
327+ line : number ,
323328 document : TextDocument
324- ) : Promise < FormatTextState > {
329+ } ) : Promise < FormatTextState > {
330+ const indentationOptions = ! editorSettings . tabSize
331+ ? ''
332+ : `key.editor.format.options: {
333+ key.editor.format.indentwidth: ${ editorSettings . tabSize } ,
334+ key.editor.format.tabwidth: ${ editorSettings . tabSize } ,
335+ key.editor.format.usetabs: 0
336+ }` ;
325337 let request = `{
326338 key.request: source.request.editor.formattext,
327- key.name: "${ keyName } ",
328- key.line: ${ keyLine } ,
329- key.length: ${ keyLength } ,
339+ key.name: "${ options . file } ",
340+ key.line: ${ options . line } ,
341+ key.length: 1,
342+ ${ indentationOptions }
330343}
331344
332345` ;
333- let firstStartPos : Position = { line : keyLine - 1 , character : 0 }
346+ let firstStartPos : Position = { line : options . line - 1 , character : 0 }
334347 let firstEndPos : Position =
335- keyLine != document . lineCount ?
336- document . positionAt (
337- document . offsetAt ( { line : keyLine , character : 0 } ) - 1 ) :
338- document . positionAt (
339- document . offsetAt ( { line : document . lineCount , character : 0 } ) )
348+ options . line != options . document . lineCount ?
349+ options . document . positionAt (
350+ options . document . offsetAt ( { line : options . line , character : 0 } ) - 1 ) :
351+ options . document . positionAt (
352+ options . document . offsetAt ( { line : options . document . lineCount , character : 0 } ) )
340353 const extraState = {
341- keyLine : keyLine ,
354+ keyLine : options . line ,
342355 lineRange : { start : firstStartPos , end : firstEndPos } //NOTE format req is 1-based
343356 }
344357 return typedResponse ( request , "editor.formattext" )
345- . then ( ( resp : string ) => {
358+ . then ( ( resp : SkFormatTextResponse ) => {
346359 const keyLine = extraState . keyLine
347360 const lineRange = extraState . lineRange
348- let offSetEnd = resp . lastIndexOf ( "\"" ) ;
349- let offSetStart = resp . indexOf ( "\"" , resp . lastIndexOf ( "key.sourcetext:" ) ) + 1
350- let lineText = resp . substring ( offSetStart , offSetEnd )
351361 return {
352362 line : keyLine ,
353363 textEdit : {
354364 range : lineRange ,
355- newText : JSON . parse ( `" ${ lineText } "` ) //NOTE convert back, silly...
365+ newText : resp [ 'key.sourcetext' ]
356366 }
357367 }
358368 } )
@@ -373,17 +383,3 @@ function debugLog(msg: string) {
373383function booleanToInt ( v : boolean ) : Number {
374384 return v ? 1 : 0
375385}
376-
377- const cutOffPrefix = "key.results:" ;
378- function cutOffResponse ( s : string ) : string {
379- s = s . substring ( s . indexOf ( cutOffPrefix ) + cutOffPrefix . length , s . length )
380- s = s . substring ( 0 , s . lastIndexOf ( "key.kind:" ) )
381- s = s . substring ( 0 , s . lastIndexOf ( "," ) ) + "]"
382- return jsonify ( s ) ;
383- }
384-
385- function jsonify ( s : string ) : string {
386- return s
387- . replace ( / ( k e y \. [ a - z _ . ] + ) : / g, '"$1":' )
388- . replace ( / ( s o u r c e \. [ a - z _ . ] + ) , / g, '"$1",' )
389- }
0 commit comments