initialize method

Future<void> initialize()

This method is used to initialize the LSP server.

This method is used internally by the CodeCrafter widget and calling it directly is not recommended. It may crash the LSP server if called multiple times.

Implementation

Future<void> initialize() async {
  final workspaceUri = Uri.directory(workspacePath).toString();
  final response = await _sendRequest(
    method: 'initialize',
    params: {
      'processId': pid,
      'rootUri': workspaceUri,
      'workspaceFolders': [
        {'uri': workspaceUri, 'name': 'workspace'},
      ],
      'capabilities': {
        'textDocument': {
          'completion': {
            'completionItem': {'snippetSupport': false},
          },
          'synchronization': {'didSave': true},
          'hover': {
            'contentFormat': ['markdown'],
          },
          'semanticTokens': {
            'dynamicRegistration': false,
            'tokenTypes': sematicMap['tokenTypes'],
            'tokenModifiers': sematicMap['tokenModifiers'],
            'formats': ['relative'],
            'requests': {
              'full': {'delta': false},
              'range': true,
            },
            'multilineTokenSupport': true,
            'overlappingTokenSupport': false,
            'augmentsSyntaxTokens': true,
          },
        },
      },
    },
  );

  if (response['error'] != null) {
    throw Exception('Initialization failed: ${response['error']}');
  }

  final capabilities = response['result']?['capabilities'];
  final semanticTokensProvider = capabilities?['semanticTokensProvider'];
  if (semanticTokensProvider != null) {
    final legend = semanticTokensProvider['legend'];
    if (legend != null) {
      _serverTokenTypes = List<String>.from(legend['tokenTypes'] ?? []);
      _serverTokenModifiers = List<String>.from(
        legend['tokenModifiers'] ?? [],
      );
    }
  }

  await _sendNotification(method: 'initialized', params: {});
}