File tree 1 file changed +8
-4
lines changed
Sources/SourceKit/sourcekitd
1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -692,6 +692,11 @@ extension SwiftLanguageServer {
692
692
req. reply ( nil )
693
693
return
694
694
}
695
+ guard let snapshot = self . documentManager. latestSnapshot ( req. params. textDocument. uri) else {
696
+ log ( " failed to find snapshot for url \( req. params. textDocument. uri) " )
697
+ req. reply ( nil )
698
+ return
699
+ }
695
700
let options = req. params. options
696
701
self . queue. async {
697
702
let configuration : SwiftFormatConfiguration . Configuration
@@ -709,14 +714,13 @@ extension SwiftLanguageServer {
709
714
710
715
let formatter = SwiftFormat . SwiftFormatter ( configuration: configuration)
711
716
do {
712
- let lines = try LineTable ( String ( contentsOf: file) )
713
- guard let lastLine = lines. last else {
717
+ guard let lastLine = snapshot. lineTable. last else {
714
718
req. reply ( nil )
715
719
return
716
720
}
717
- let lastPosition = Position ( line: lines . count- 1 , utf16index: lastLine. utf16. count)
721
+ let lastPosition = Position ( line: snapshot . lineTable . count- 1 , utf16index: lastLine. utf16. count)
718
722
var edit = TextEdit ( range: Position ( line: 0 , utf16index: 0 ) ..< lastPosition, newText: " " )
719
- try formatter. format ( contentsOf : file, to: & edit. newText)
723
+ try formatter. format ( source : snapshot . text , assumingFileURL : file, to: & edit. newText)
720
724
req. reply ( [ edit] )
721
725
} catch {
722
726
log ( " failed to format document: \( error) " , level: . error)
You can’t perform that action at this time.
0 commit comments