Skip to content

Commit ca48313

Browse files
Added notification on file open
1 parent 95479a5 commit ca48313

File tree

4 files changed

+61
-13
lines changed

4 files changed

+61
-13
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3924,7 +3924,6 @@
39243924
6CD3CA542C8B508200D83DCD /* CodeEditSourceEditor */,
39253925
6CB94D022CA1205100E8651C /* AsyncAlgorithms */,
39263926
6CC00A8A2CBEF150004E8134 /* CodeEditSourceEditor */,
3927-
6C05CF9D2CDE8699006AAECD /* CodeEditSourceEditor */,
39283927
30818CB42D4E563900967860 /* ZIPFoundation */,
39293928
);
39303929
productName = CodeEdit;
@@ -4023,8 +4022,8 @@
40234022
303E88462C276FD600EEA8D9 /* XCRemoteSwiftPackageReference "LanguageServerProtocol" */,
40244023
6C4E37FA2C73E00700AEE7B5 /* XCRemoteSwiftPackageReference "SwiftTerm" */,
40254024
6CB94D012CA1205100E8651C /* XCRemoteSwiftPackageReference "swift-async-algorithms" */,
4026-
6C05CF9C2CDE8699006AAECD /* XCRemoteSwiftPackageReference "CodeEditSourceEditor" */,
40274025
30818CB32D4E563900967860 /* XCRemoteSwiftPackageReference "ZIPFoundation" */,
4026+
30C549D82D77BDF8008DDEF8 /* XCRemoteSwiftPackageReference "CodeEditSourceEditor" */,
40284027
);
40294028
productRefGroup = B658FB2D27DA9E0F00EA4DBD /* Products */;
40304029
projectDirPath = "";
@@ -5875,6 +5874,14 @@
58755874
minimumVersion = 0.9.19;
58765875
};
58775876
};
5877+
30C549D82D77BDF8008DDEF8 /* XCRemoteSwiftPackageReference "CodeEditSourceEditor" */ = {
5878+
isa = XCRemoteSwiftPackageReference;
5879+
repositoryURL = "https://github.com/CodeEditApp/CodeEditSourceEditor.git";
5880+
requirement = {
5881+
kind = upToNextMajorVersion;
5882+
minimumVersion = 0.10.0;
5883+
};
5884+
};
58785885
30CB648F2C16CA8100CC8A9E /* XCRemoteSwiftPackageReference "LanguageServerProtocol" */ = {
58795886
isa = XCRemoteSwiftPackageReference;
58805887
repositoryURL = "https://github.com/ChimeHQ/LanguageServerProtocol";

CodeEdit.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodeEdit/Features/LSP/Registry/RegistryManager.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -135,18 +135,30 @@ final class RegistryManager {
135135

136136
/// Loads registry items from disk
137137
private func loadItemsFromDisk() -> [RegistryItem]? {
138+
let registryPath = saveLocation.appending(path: "registry.json")
139+
let fileManager = FileManager.default
140+
141+
// Update the file every 24 hours
142+
let needsUpdate = !fileManager.fileExists(atPath: registryPath.path) || {
143+
guard let attributes = try? fileManager.attributesOfItem(atPath: registryPath.path),
144+
let modificationDate = attributes[.modificationDate] as? Date else {
145+
return true
146+
}
147+
let hoursSinceLastUpdate = Date().timeIntervalSince(modificationDate) / 3600
148+
return hoursSinceLastUpdate > 24
149+
}()
150+
151+
if needsUpdate {
152+
Task { await update() }
153+
return nil
154+
}
155+
138156
do {
139-
let registryPath = saveLocation.appending(path: "registry.json")
140157
let registryData = try Data(contentsOf: registryPath)
141-
let decoder = JSONDecoder()
142-
let items = try decoder.decode([RegistryItem].self, from: registryData)
143-
return items.filter {
144-
$0.categories.contains("LSP")
145-
}
158+
let items = try JSONDecoder().decode([RegistryItem].self, from: registryData)
159+
return items.filter { $0.categories.contains("LSP") }
146160
} catch {
147-
Task {
148-
await update()
149-
}
161+
Task { await update() }
150162
return nil
151163
}
152164
}

CodeEdit/Features/LSP/Service/LSPService.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import Foundation
1111
import LanguageClient
1212
import LanguageServerProtocol
1313
import CodeEditLanguages
14+
import SwiftUICore
1415

1516
/// `LSPService` is a service class responsible for managing the lifecycle and event handling
1617
/// of Language Server Protocol (LSP) clients within the CodeEdit application. It handles the initialization,
@@ -121,6 +122,9 @@ final class LSPService: ObservableObject {
121122
@AppSettings(\.developerSettings.lspBinaries)
122123
var lspBinaries
123124

125+
@Environment(\.openWindow)
126+
private var openWindow
127+
124128
init() {
125129
// Load the LSP binaries from the developer menu
126130
for binary in lspBinaries {
@@ -211,6 +215,7 @@ final class LSPService: ObservableObject {
211215
languageServer = try await self.startServer(for: lspLanguage, workspacePath: workspacePath)
212216
}
213217
} catch {
218+
notifyToInstallLanguageServer(language: lspLanguage)
214219
// swiftlint:disable:next line_length
215220
self.logger.error("Failed to find/start server for language: \(lspLanguage.rawValue), workspace: \(workspacePath, privacy: .private)")
216221
return
@@ -310,6 +315,30 @@ final class LSPService: ObservableObject {
310315
}
311316
}
312317

318+
extension LSPService {
319+
private func notifyToInstallLanguageServer(language lspLanguage: LanguageIdentifier) {
320+
let lspLanguageTitle = lspLanguage.rawValue.capitalized
321+
let notificationTitle = "Install \(lspLanguageTitle) Language Server"
322+
// Make sure the user doesn't have the same existing notification
323+
guard !NotificationManager.shared.notifications.contains(where: { $0.title == notificationTitle }) else {
324+
return
325+
}
326+
327+
NotificationManager.shared.post(
328+
iconSymbol: "arrow.down.circle",
329+
iconColor: .clear,
330+
title: notificationTitle,
331+
description: "Install the \(lspLanguageTitle) language server to enable code intelligence features.",
332+
actionButtonTitle: "Install"
333+
) { [weak self] in
334+
// TODO: Warning:
335+
// Accessing Environment<OpenWindowAction>'s value outside of being installed on a View.
336+
// This will always read the default value and will not update
337+
self?.openWindow(sceneID: .settings)
338+
}
339+
}
340+
}
341+
313342
// MARK: - Errors
314343

315344
enum ServerManagerError: Error {

0 commit comments

Comments
 (0)