Class: MilkTea::LSP::Workspace

Inherits:
Object
  • Object
show all
Includes:
WorkspaceAnalysis, WorkspaceCaches, WorkspaceCollection, WorkspaceDefinitionIndex, WorkspaceDependencyGraph, WorkspaceStore, WorkspaceUtilities
Defined in:
lib/milk_tea/lsp/workspace.rb,
lib/milk_tea/lsp/workspace/store.rb,
lib/milk_tea/lsp/workspace/caches.rb,
lib/milk_tea/lsp/workspace/analysis.rb,
lib/milk_tea/lsp/workspace/utilities.rb,
lib/milk_tea/lsp/workspace/collection.rb,
lib/milk_tea/lsp/workspace/definition_index.rb,
lib/milk_tea/lsp/workspace/dependency_graph.rb

Overview

Manages open documents, AST cache, token cache, semantic facts cache, and symbol index. Supports incremental document edits and workspace-wide indexing.

Defined Under Namespace

Modules: WorkspaceAnalysis, WorkspaceCaches, WorkspaceCollection, WorkspaceDefinitionIndex, WorkspaceDependencyGraph, WorkspaceStore, WorkspaceUtilities

Constant Summary collapse

DOCUMENT_SOURCES =
%w[active-editor visible-editor background-document].freeze
PERF_LOG_THRESHOLD_MS =
1000
DEFINITION_KEYWORDS =

Token types that introduce a named definition, in order of precedence.

NOTE: this list is intentionally minimal. Multi-keyword prefixes such as 'const function', 'async function', 'external function', 'foreign function', and visibility modifiers ('public') are captured by the adjacent-token scan in extract_symbols_from_tokens because the scan relies on every pair of adjacent tokens, and the inner keyword (e.g. :function) is always followed by the identifier. If the lexer ever merges a compound keyword into a single token type (e.g. :const_function), it must be added here.

%i[function struct union enum flags variant type const var let extending opaque interface event].freeze
DOC_COMMENT_PREFIX =
'##'
DOC_TAG_PATTERN =
/\A\s*@([A-Za-z_][A-Za-z0-9_-]*)(?:\s+(.*))?\z/
DEFINITION_LINE_PREFIX =
/^(?:\s)*(?:(?:public|foreign|external)\s+)*(?:function|struct|union|enum|flags|variant|type|const|var|let|extending|opaque|interface|event)\s+/m
DEFINITION_NAME_REGEX =
/^\s*(?:(?:public|foreign|external)\s+)*(?:function|struct|union|enum|flags|variant|type|const|var|let|extending|opaque|interface|event)\s+([A-Za-z_][A-Za-z0-9_]*)\b/

Instance Method Summary collapse

Methods included from WorkspaceUtilities

#dependency_resolution_mode, #dependency_resolution_mode=, #find_call_context, #find_definition_token, #find_dot_receiver, #find_dot_receiver_path, #find_token_at, #platform_override, #platform_override=, #shared_module_cache, #strict_current_root_diagnostics_enabled, #strict_current_root_diagnostics_enabled=, #token_contains_position?, #workspace_root_path, #workspace_root_path=

Methods included from WorkspaceCollection

#collect_diagnostics

Methods included from WorkspaceDefinitionIndex

#find_definition_token_global

Methods included from WorkspaceDependencyGraph

#apply_watched_file_change, #open_document_uris, #refresh_open_document_dependency_caches, #related_open_document_uris, #reverse_import_dependents_for

Methods included from WorkspaceCaches

#all_documents, #doc_comment_data_for_definition, #doc_comment_for_definition, #find_all_references, #find_all_references_in, #get_ast, #get_content, #get_facts, #get_symbols, #get_tokens, #get_tooling_snapshot, #index_identifier_tokens, #module_name_for_uri, #position_to_offset, #remove_identifier_index_entries

Methods included from WorkspaceStore

#apply_incremental_change_to_content, #apply_incremental_changes, #apply_incremental_changes_against_snapshot, #background_document?, #close_document, #document_source, #index_workspace, #open_document, #rename_indexed_file, #set_document_source, #shutdown

Constructor Details

#initializeWorkspace

Returns a new instance of Workspace.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/milk_tea/lsp/workspace.rb', line 39

def initialize
  @workspace_root_path = nil
  @dependency_resolution_mode = :auto
  @platform_override = nil
  @strict_current_root_diagnostics_enabled = false
  @open_documents = {}   # uri -> content String from didOpen/didChange
  @indexed_documents = {} # uri -> content String loaded from disk index
  @document_sources = {} # uri -> source string from the editor client
  @document_state_mutex = Mutex.new
  @tokens_cache = {}   # uri -> [Token]
  @last_good_tokens_cache = {} # uri -> last known-good [Token]
  @ast_cache = {}      # uri -> AST::SourceFile (nil on parse failure)
  @facts_cache = {} # uri -> SemanticAnalyzer::Facts (projection of cached tooling snapshot facts)
  @tooling_snapshot_cache = {} # uri -> SemanticAnalyzer::ToolingSnapshot (facts may be nil on structural failure)
  @symbols_cache = {}  # uri -> [{name, kind, line, column}]
  @doc_comments_cache = {} # uri -> {"line:column" => structured_doc_comment_hash}
  @last_good_facts_cache = {} # uri -> last SemanticAnalyzer::Facts that succeeded
  @last_good_tooling_snapshot_cache = {} # uri -> last SemanticAnalyzer::ToolingSnapshot with facts that succeeded
  @document_module_names = {} # uri -> module name string (populated from last-good facts)
  @shared_module_cache = {}
  @facts_cache_mutex = Mutex.new
  @facts_generation = Hash.new(0)
  @facts_state_mutex = Mutex.new
  # Diagnostics cache: uri -> { content_hash:, diagnostics: }
  @diagnostics_cache = {}
  @dependency_module_name_by_uri = {}
  @dependency_imports_by_uri = {}
  @reverse_import_dependents = Hash.new { |hash, key| hash[key] = Set.new }
  @full_reverse_index_built = false
  @full_reverse_index_built = false
  # Definition index: name -> { uri:, token: } — built lazily from symbols cache.
  # Caches known matching definitions without forcing a full-workspace index
  # build on the first global lookup.
  @definition_index = {} # name -> [{ uri:, token: Token }]
  @definition_miss_cache = Set.new
  @definition_candidate_uris = Hash.new { |hash, key| hash[key] = Set.new }
  @definition_names_by_uri = {}
  @definition_cache_mutex = Mutex.new
  @definition_warmup_queue = Queue.new
  @definition_warmup_enqueued = Set.new
  @definition_warmup_thread = nil
  # Identifier index: name -> [{uri:, line:, col:}] — lazily populated from tokens
  @identifier_index = {}
  @identifier_index_mutex = Mutex.new
  @indexed_uris = Set.new
end

Instance Method Details

#cross_file_diagnosticsObject



99
100
101
# File 'lib/milk_tea/lsp/workspace/collection.rb', line 99

def cross_file_diagnostics
  @facts_cache_mutex.synchronize { @cross_file_diagnostics }
end

#resetObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/milk_tea/lsp/workspace.rb', line 86

def reset
  @workspace_root_path = nil
  @dependency_resolution_mode = :auto
  @platform_override = nil
  @strict_current_root_diagnostics_enabled = false
  @indexed_documents.clear
  @tokens_cache.clear
  @last_good_tokens_cache.clear
  @ast_cache.clear
  @facts_cache.clear
  @tooling_snapshot_cache.clear
  @symbols_cache.clear
  @doc_comments_cache.clear
  @last_good_facts_cache.clear
  @last_good_tooling_snapshot_cache.clear
  @document_module_names.clear
  @shared_module_cache.clear
  @definition_index.clear
  @identifier_index.clear
  @full_reverse_index_built = false
  @indexed_uris.clear
  @definition_miss_cache.clear
  @definition_candidate_uris.clear
  @definition_names_by_uri.clear
  @definition_warmup_enqueued.clear
  @dependency_module_name_by_uri.clear
  @dependency_imports_by_uri.clear
  @reverse_import_dependents.clear
  @diagnostics_cache.clear
  @facts_generation.clear
end