Skip to content

Commit

Permalink
Merge pull request #200 from scade-platform/reload-lsp-updated
Browse files Browse the repository at this point in the history
Reload LSP for new files
  • Loading branch information
Kristalev authored Dec 24, 2021
2 parents ab2c662 + e52cf79 commit 1e3dc47
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 27 deletions.
4 changes: 2 additions & 2 deletions Packages/LSPClient/Sources/LSPClient/LSPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class LSPClient {

private(set) var state: State = .ready

public private(set) var workspaceFolders: [URL] = []
public private(set) var workspaceFolders: [Folder] = []

public private(set) var openedDocuments: [ObjectIdentifier: SourceCodeDocumentRef] = [:]

Expand All @@ -58,7 +58,7 @@ public final class LSPClient {
// the first folder as the root folder per default
guard state == .ready else { return }

self.workspaceFolders = workspaceFolders
self.workspaceFolders = workspaceFolders.compactMap { Folder(url: $0) }

var rootURI: DocumentURI? = nil
if let rootURL = workspaceFolders.first {
Expand Down
68 changes: 43 additions & 25 deletions Packages/LSPClient/Sources/LSPClient/LSPConnector.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,16 @@ public final class LSPConnector {
}
}

observeWorkspaceFolders(for: server.client)

return server
}

private func observeWorkspaceFolders(for client: LSPClient) {
for folder in client.workspaceFolders {
folder.observers.add(observer: self)
}
}

private func findServer(for doc: SourceCodeDocument) -> (LSPServer?, Folder?) {
guard let proj = workbench?.project,
Expand All @@ -91,11 +99,31 @@ public final class LSPConnector {
}

let runningServer = runningServers[doc.languageId]?.first {
$0.client.workspaceFolders.contains(folder.path.url)
$0.client.workspaceFolders.map{$0.url}.contains(folder.path.url)
}

return (runningServer, folder)
}

private func restart(server: LSPServer, lang: String) {
guard let workbench = workbench else { return }
// Store current context
let workspaceFoldersURL = server.client.workspaceFolders.map{$0.url}

// Stop the servers
server.stop()
self.runningServers[lang]?.removeAll{ $0 === server }

// Restart with the previous context
if let server = self.createServer(for: lang, workspaceFolders: workspaceFoldersURL) {
workbench.documents.compactMap {$0 as? SourceCodeDocument}.forEach { doc in
guard let docPath = doc.fileURL?.absoluteString,
workspaceFoldersURL.contains(where: {folderUrl in docPath.hasPrefix(folderUrl.absoluteString)}) else { return }

server.client.openDocument(doc: doc)
}
}
}
}


Expand Down Expand Up @@ -152,36 +180,26 @@ extension LSPConnector: BuildSystemsObserver {
public func workbenchDidChangeVariant(_ workbench: Workbench, variant: Variant?) {
guard self.workbench === workbench else { return }

// let langServers = self.runningServers.compactMap {(lang, servers) -> (String, [LSPServer])? in
// let filteredServers = servers.filter { server in
// server.client.workspaceFolders.contains { target.contains(url: $0) }
// }
//
// return filteredServers.count > 0 ? (lang, filteredServers) : nil
// }

for (lang, servers) in self.runningServers {
servers.forEach { server in
// Check if the server should be restarted for the provided variant
guard server.shouldRestart(for: variant) else { return }

// Store current context
let workspaceFolders = server.client.workspaceFolders

// Stop the servers
server.stop()
self.runningServers[lang]?.removeAll{ $0 === server }

// Restart with the previous context
if let server = self.createServer(for: lang, workspaceFolders: workspaceFolders) {
workbench.documents.compactMap {$0 as? SourceCodeDocument}.forEach { doc in
guard let docPath = doc.fileURL?.absoluteString,
workspaceFolders.contains(where: {folderUrl in docPath.hasPrefix(folderUrl.absoluteString)}) else { return }

server.client.openDocument(doc: doc)
}
}
restart(server: server, lang: lang)
}
}
}
}

extension LSPConnector : FolderObserver {
public func childDidChange(_ folder: Folder, child: Path) {
for (lang, servers) in self.runningServers {
servers.forEach { server in
guard !DocumentManager.shared.documentIsOpen(path: child) else { return }
let shouldRestart = server.client.workspaceFolders.contains{ $0 == folder } && !child.string.contains(".build")
guard shouldRestart else { return }
restart(server: server, lang: lang)
}
}
}
}
8 changes: 8 additions & 0 deletions Packages/NimbleCore/Sources/NimbleCore/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,14 @@ public class DocumentManager {

return docRef?.ref.value as? Document
}

public func documentIsOpen(path: Path) -> Bool {
guard let file = File(path: path), let type = selectDocumentClass(for: file) else {
return false
}

return searchOpenedDocument(path, docType: type) != nil
}
}

// MARK: - DocumentSessionState
Expand Down

0 comments on commit 1e3dc47

Please sign in to comment.