-
Notifications
You must be signed in to change notification settings - Fork 314
Open documents using testClient.openDocument
instead of constructing a DidOpenDocumentNotification
#910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open documents using testClient.openDocument
instead of constructing a DidOpenDocumentNotification
#910
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2023 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/// Finds all marked ranges in the given text, see `Marker`. | ||
fileprivate func findMarkedRanges(text: String) -> [Marker] { | ||
var markers = [Marker]() | ||
while let marker = nextMarkedRange(text: text, from: markers.last?.range.upperBound ?? text.startIndex) { | ||
markers.append(marker) | ||
} | ||
return markers | ||
} | ||
|
||
extension Character { | ||
var isMarkerEmoji: Bool { | ||
switch self { | ||
case "0️⃣", "1️⃣", "2️⃣", "3️⃣", "4️⃣", "5️⃣", "6️⃣", "7️⃣", "8️⃣", "9️⃣", "🔟", "ℹ️": | ||
return true | ||
default: return false | ||
} | ||
} | ||
} | ||
|
||
fileprivate func nextMarkedRange(text: String, from: String.Index) -> Marker? { | ||
guard let start = text[from...].firstIndex(where: { $0.isMarkerEmoji }) else { | ||
return nil | ||
} | ||
let end = text.index(after: start) | ||
|
||
let markerRange = start..<end | ||
let name = text[start..<end] | ||
|
||
return Marker(name: name, range: markerRange) | ||
} | ||
|
||
fileprivate struct Marker { | ||
/// The name of the marker. | ||
let name: Substring | ||
/// The range of the marker. | ||
/// | ||
/// If the marker contains all the non-whitespace characters on the line, | ||
/// this is the range of the entire line. Otherwise it's the range of the | ||
/// marker itself. | ||
let range: Range<String.Index> | ||
} | ||
|
||
public func extractMarkers(_ markedText: String) -> (markers: [String: Int], textWithoutMarkers: String) { | ||
var text = "" | ||
var markers = [String: Int]() | ||
var lastIndex = markedText.startIndex | ||
for marker in findMarkedRanges(text: markedText) { | ||
text += markedText[lastIndex..<marker.range.lowerBound] | ||
lastIndex = marker.range.upperBound | ||
|
||
assert(markers[String(marker.name)] == nil, "Marker names must be unique") | ||
markers[String(marker.name)] = text.utf8.count | ||
} | ||
text += markedText[lastIndex..<markedText.endIndex] | ||
|
||
return (markers, text) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
import LanguageServerProtocol | ||
|
||
extension Language { | ||
var fileExtension: String { | ||
switch self { | ||
case .objective_c: return "m" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, I wanted to add a case here once/if we ever have an Objective-C++ test case. |
||
default: return self.rawValue | ||
} | ||
} | ||
|
||
init?(fileExtension: String) { | ||
switch fileExtension { | ||
case "m": self = .objective_c | ||
default: self.init(rawValue: fileExtension) | ||
} | ||
} | ||
} | ||
|
||
extension DocumentURI { | ||
/// Create a unique URI for a document of the given language. | ||
public static func `for`(_ language: Language, testName: String = #function) -> DocumentURI { | ||
let testBaseName = testName.prefix(while: \.isLetter) | ||
|
||
#if os(Windows) | ||
let url = URL(fileURLWithPath: "C:/\(testBaseName)/\(UUID())/test.\(language.fileExtension)") | ||
#else | ||
let url = URL(fileURLWithPath: "/\(testBaseName)/\(UUID())/test.\(language.fileExtension)") | ||
#endif | ||
return DocumentURI(url) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I haven't checked, is this basically just a copy paste from swift-syntax 😅?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this is a copy-paste from swift-syntax 😢