File tree Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Expand file tree Collapse file tree 2 files changed +24
-2
lines changed Original file line number Diff line number Diff line change 12
12
13
13
import ArgumentParser
14
14
import Foundation
15
+ import SKSupport
15
16
import SourceKitD
16
17
17
18
import struct TSCBasic. AbsolutePath
@@ -35,15 +36,32 @@ public struct SourceKitdRequestCommand: AsyncParsableCommand {
35
36
)
36
37
var sourcekitdRequestPath : String
37
38
39
+ @Option ( help: " line:column override for key.offset " )
40
+ var position : String ?
41
+
38
42
public init ( ) { }
39
43
40
44
public func run( ) async throws {
41
- let requestString = try String ( contentsOf: URL ( fileURLWithPath: sourcekitdRequestPath) )
45
+ var requestString = try String ( contentsOf: URL ( fileURLWithPath: sourcekitdRequestPath) )
42
46
43
47
let sourcekitd = try DynamicallyLoadedSourceKitD . getOrCreate (
44
48
dylibPath: try ! AbsolutePath ( validating: sourcekitdPath)
45
49
)
46
50
51
+ if let lineColumn = position? . split ( separator: " : " , maxSplits: 2 ) . map ( Int . init) ,
52
+ lineColumn. count == 2 ,
53
+ let line = lineColumn [ 0 ] ,
54
+ let column = lineColumn [ 1 ]
55
+ {
56
+ let requestInfo = try RequestInfo ( request: requestString)
57
+
58
+ let lineTable = LineTable ( requestInfo. fileContents)
59
+ if let offset = lineTable. utf8OffsetOf ( line: line - 1 , utf8Column: column - 1 ) {
60
+ print ( " Adjusting request offset to \( offset) " )
61
+ requestString. replace ( #/key.offset: [0-9]+/# , with: " key.offset: \( offset) " )
62
+ }
63
+ }
64
+
47
65
let request = try requestString. cString ( using: . utf8) !. withUnsafeBufferPointer { buffer in
48
66
var error : UnsafeMutablePointer < CChar > ?
49
67
let req = sourcekitd. api. request_create_from_yaml ( buffer. baseAddress!, & error) !
Original file line number Diff line number Diff line change @@ -147,10 +147,14 @@ extension LineTable {
147
147
/// - parameter utf8Column: UTF-8 column offset (zero-based).
148
148
@inlinable
149
149
public func stringIndexOf( line: Int , utf8Column: Int ) -> String . Index ? {
150
- guard line < count else {
150
+ guard 0 <= line , line < count else {
151
151
// Line out of range.
152
152
return nil
153
153
}
154
+ guard 0 <= utf8Column else {
155
+ // Column out of range.
156
+ return nil
157
+ }
154
158
let lineSlice = self [ line]
155
159
return content. utf8. index ( lineSlice. startIndex, offsetBy: utf8Column, limitedBy: lineSlice. endIndex)
156
160
}
You can’t perform that action at this time.
0 commit comments