Skip to content

Commit

Permalink
- Converted Dart LSP provider to an (Async)NotifierProvider that fixe…
Browse files Browse the repository at this point in the history
…s issue in #9
  • Loading branch information
5hirish committed Aug 5, 2023
1 parent aaded52 commit 1c4aadd
Show file tree
Hide file tree
Showing 12 changed files with 116 additions and 95 deletions.
3 changes: 2 additions & 1 deletion lib/provider/code.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/provider/datastore.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/provider/file.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

93 changes: 84 additions & 9 deletions lib/provider/lsp.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,97 @@
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path/path.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../logger.dart';
import '../models/log_level.dart';
import '../models/lsp/error.dart';
import '../models/lsp/params/capabilities.dart';
import '../models/lsp/params/clientInfo.dart';
import '../models/lsp/params/initializationOptions.dart';
import '../models/lsp/params/initialize.dart';
import '../models/lsp/params/workspaceFolder.dart';
import '../services/lsp/lang/dart.dart';
import '../utils.dart';
import '../wrapper/process.dart';
import 'notifications.dart';

part 'lsp.g.dart';

@Riverpod(keepAlive: true)
Future<DartLSPService> dartLSP(DartLSPRef ref) async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();
class DartLSP extends _$DartLSP {
Future<DartLSPService> startDartLSP() async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();

DartLSPService service = await DartLSPService.start(
processWrapper: ActualProcessWrapper(),
clientId: packageInfo.packageName,
clientVersion: packageInfo.version,
logFilePath: '${packageInfo.packageName}-dart-sdk-lsp.log');
DartLSPService service = await DartLSPService.start(
processWrapper: ActualProcessWrapper(),
clientId: packageInfo.packageName,
clientVersion: packageInfo.version,
logFilePath: '${packageInfo.packageName}-dart-sdk-lsp.log');

ref.onDispose(service.stop);
ref.onDispose(service.stop);

return service;
return service;
}

@override
Future<DartLSPService> build() async {
return startDartLSP();
}

void initializeLSP(String directoryPath) async {
if (!state.hasValue) {
await startDartLSP();
}

DartLSPService lspDart = state.requireValue;
PackageInfo packageInfo = await PackageInfo.fromPlatform();

int lspParentProcessId = await lspDart.getParentProcessId();

final directoryUri = Uri.parse(directoryPath);
WorkspaceFolder workspaceFolder = WorkspaceFolder(
uri: directoryUri,
name: basename(directoryPath),
);

Initialize initialize = Initialize(
processId: lspParentProcessId,
rootUri: directoryUri.toString(),
capabilities: clientCapabilities,
initializationOptions: const InitializationOptions(),
trace: "verbose",
workspaceFolder: [workspaceFolder],
clientInfo: ClientInfo(
name: packageInfo.packageName, version: packageInfo.version),
locale: getCurrentLocale(),
);

try {
Map<String, dynamic> initialized = await lspDart.initialize(initialize);
logger.i("LSP:initialize: >>>");

if (initialized['capabilities'] != null &&
initialized['serverInfo'] != null) {
lspDart.initialized();
logger.i("LSP:initialized: <<< ");

ref.watch(inAppNotificationStateProvider.notifier).fireInNotification(
'Dart LSP server initialized!',
logLevel: LogLevel.info,
);
}
} catch (err) {
if (err is LSPError) {
if (err.code == -32002) {
// Todo: Call change directory path
} else {
ref.watch(inAppNotificationStateProvider.notifier).fireInNotification(
'Failed to initialize Dart LSP server',
logLevel: LogLevel.fatal,
);
}
}
logger.e(err);
}
}
}
15 changes: 8 additions & 7 deletions lib/provider/lsp.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions lib/provider/notifications.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

72 changes: 4 additions & 68 deletions lib/provider/project.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
import 'dart:io';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:path/path.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../logger.dart';
import '../models/ignored.dart';
import '../models/log_level.dart';
import '../models/lsp/error.dart';
import '../models/lsp/params/capabilities.dart';
import '../models/lsp/params/clientInfo.dart';
import '../models/lsp/params/initializationOptions.dart';
import '../models/lsp/params/initialize.dart';
import '../models/lsp/params/workspaceFolder.dart';
import '../utils.dart';
import 'lsp.dart';
import 'notifications.dart';

part 'project.g.dart';

Expand All @@ -26,66 +15,13 @@ class ProjectDirectoryPath extends _$ProjectDirectoryPath {
}

void changeDirectoryPath(String? directoryPath) {
initializeLSP(directoryPath!);
// Reading the dartLSPProvider.future will start the LSP service if it hasn't been started yet
// Reading dartLSPProvider as its used inside functions triggered by user interactions (User select workspace)
ref.read(dartLSPProvider.future).then((dartLSPService) =>
ref.read(dartLSPProvider.notifier).initializeLSP(directoryPath!));

state = directoryPath;
}

void initializeLSP(String directoryPath) async {
ref.read(dartLSPProvider).whenData((lspDart) async {
PackageInfo packageInfo = await PackageInfo.fromPlatform();

int lspParentProcessId = await lspDart.getParentProcessId();

final directoryUri = Uri.parse(directoryPath);
WorkspaceFolder workspaceFolder = WorkspaceFolder(
uri: directoryUri,
name: basename(directoryPath),
);

Initialize initialize = Initialize(
processId: lspParentProcessId,
rootUri: directoryUri.toString(),
capabilities: clientCapabilities,
initializationOptions: const InitializationOptions(),
trace: "verbose",
workspaceFolder: [workspaceFolder],
clientInfo: ClientInfo(
name: packageInfo.packageName, version: packageInfo.version),
locale: getCurrentLocale(),
);

try {
Map<String, dynamic> initialized = await lspDart.initialize(initialize);
logger.i("LSP:initialize: >>>");

if (initialized['capabilities'] != null &&
initialized['serverInfo'] != null) {
lspDart.initialized();
logger.i("LSP:initialized: <<< ");

ref.watch(inAppNotificationStateProvider.notifier).fireInNotification(
'Dart LSP server initialized!',
logLevel: LogLevel.info,
);
}
} catch (err) {
if (err is LSPError) {
if (err.code == -32002) {
// Todo: Call change directory path
} else {
ref
.watch(inAppNotificationStateProvider.notifier)
.fireInNotification(
'Failed to initialize Dart LSP server',
logLevel: LogLevel.fatal,
);
}
}
logger.e(err);
}
});
}
}

@Riverpod(keepAlive: true)
Expand Down
5 changes: 3 additions & 2 deletions lib/provider/project.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/provider/repository.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/provider/theme.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/provider/tools.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/router/router.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1c4aadd

Please sign in to comment.