<commit_before>import 'dart:async';
import 'dart:io';

import 'package:analyzer/exception/exception.dart';
import 'package:collection/collection.dart';
import 'package:compiler/compiler.dart';
import 'package:dartx/dartx.dart' hide Range;
import 'package:path/path.dart' as p;

import 'channel.dart';
import 'error_codes.dart';
import 'generated/lsp_protocol/protocol_generated.dart';
import 'generated/lsp_protocol/protocol_special.dart';
import 'handlers/handlers.dart';
import 'handlers/states.dart';
import 'overlay_resource_provider.dart';
import 'type_labels.dart';
import 'utils.dart';

class AnalysisServer {
  // Section: Lifecycle

  /// Initialize a newly created server to send and receive messages to the
  /// given [channel].
  AnalysisServer(this.channel, this.candyDirectory)
      : assert(channel != null),
        assert(candyDirectory != null) {
    messageHandler = UninitializedStateMessageHandler(this);

    channel.listen(
      _handleMessage,
      onDone: () {},
      onError: sendSocketErrorNotification,
    );
    sendLogMessage('Started Candy LSP');
  }

  /// The channel from which messages are received and to which responses should
  /// be sent.
  final LspServerCommunicationChannel channel;
  final Directory candyDirectory;

  ServerStateMessageHandler messageHandler;

  ClientCapabilities _clientCapabilities;

  /// The capabilities of the LSP client. Will be null prior to initialization.
  ClientCapabilities get clientCapabilities => _clientCapabilities;

  LspInitializationOptions _initializationOptions;

  /// Initialization options provided by the LSP client. Allows opting in/out of
  /// specific server functionality. Will be null prior to initialization.
  LspInitializationOptions get initializationOptions => _initializationOptions;
  void handleClientConnection(
    ClientCapabilities capabilities,
    dynamic initializationOptions,
    Directory projectDirectory,
  ) {
    _clientCapabilities = capabilities;
    _initializationOptions = LspInitializationOptions(initializationOptions);
    _projectDirectory = projectDirectory;
    _resourceProvider = OverlayResourceProvider(
      ResourceProvider.default_(
        candyDirectory: candyDirectory,
        projectDirectory: projectDirectory,
      ),
    );
    _queryConfig = QueryConfig(
      packageName: 'core',
      resourceProvider: resourceProvider,
      buildArtifactManager: BuildArtifactManager(projectDirectory),
    );
  }

  /// Capabilities of the server. Will be null prior to initialization as
  /// the server capabilities depend on the client capabilities.
  ServerCapabilities capabilities;

  /// Whether or not the server is controlling the shutdown and will exit
  /// automatically.<commit_msg>feat(packages/lsp_server): get project name from path<commit_after>import 'dart:async';
import 'dart:io';

import 'package:analyzer/exception/exception.dart';
import 'package:collection/collection.dart';
import 'package:compiler/compiler.dart';
import 'package:dartx/dartx.dart' hide Range;
import 'package:path/path.dart' as p;

import 'channel.dart';
import 'error_codes.dart';
import 'generated/lsp_protocol/protocol_generated.dart';
import 'generated/lsp_protocol/protocol_special.dart';
import 'handlers/handlers.dart';
import 'handlers/states.dart';
import 'overlay_resource_provider.dart';
import 'type_labels.dart';
import 'utils.dart';

class AnalysisServer {
  // Section: Lifecycle

  /// Initialize a newly created server to send and receive messages to the
  /// given [channel].
  AnalysisServer(this.channel, this.candyDirectory)
      : assert(channel != null),
        assert(candyDirectory != null) {
    messageHandler = UninitializedStateMessageHandler(this);

    channel.listen(
      _handleMessage,
      onDone: () {},
      onError: sendSocketErrorNotification,
    );
    sendLogMessage('Started Candy LSP');
  }

  /// The channel from which messages are received and to which responses should
  /// be sent.
  final LspServerCommunicationChannel channel;
  final Directory candyDirectory;

  ServerStateMessageHandler messageHandler;

  ClientCapabilities _clientCapabilities;

  /// The capabilities of the LSP client. Will be null prior to initialization.
  ClientCapabilities get clientCapabilities => _clientCapabilities;

  LspInitializationOptions _initializationOptions;

  /// Initialization options provided by the LSP client. Allows opting in/out of
  /// specific server functionality. Will be null prior to initialization.
  LspInitializationOptions get initializationOptions => _initializationOptions;
  void handleClientConnection(
    ClientCapabilities capabilities,
    dynamic initializationOptions,
    Directory projectDirectory,
  ) {
    _clientCapabilities = capabilities;
    _initializationOptions = LspInitializationOptions(initializationOptions);
    _projectDirectory = projectDirectory;
    _resourceProvider = OverlayResourceProvider(
      ResourceProvider.default_(
        candyDirectory: candyDirectory,
        projectDirectory: projectDirectory,
      ),
    );
    _queryConfig = QueryConfig(
      packageName: p.basename(projectDirectory.absolute.path),
      resourceProvider: resourceProvider,
      buildArtifactManager: BuildArtifactManager(projectDirectory),
    );
  }

  /// Capabilities of the server. Will be null prior to initialization as
  /// the server capabilities depend on the client capabilities.
  ServerCapabilities capabilities;

  /// Whether or not the server is controlling the shutdown and will exit
  /// automatically.
{'fraction_non_alphanumeric': 0.0827027027027027, 'fraction_numerical': 0.0, 'mean_word_length': 4.431506849315069, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7052104', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1413, 'n_words': 402}