11from functools import lru_cache
22from sqlglot import Dialect , Tokenizer
3- from sqlmesh .lsp .custom import AllModelsResponse , MacroCompletion
3+ from sqlmesh .lsp .custom import (
4+ AllModelsResponse ,
5+ MacroCompletion ,
6+ ModelCompletion ,
7+ )
48from sqlmesh import macro
59import typing as t
610from sqlmesh .lsp .context import AuditTarget , LSPContext , ModelTarget
11+ from sqlmesh .lsp .description import generate_markdown_description
712from sqlmesh .lsp .uri import URI
813
914
@@ -26,38 +31,42 @@ def get_sql_completions(
2631 # Combine keywords - SQL keywords first, then file keywords
2732 all_keywords = list (sql_keywords ) + list (file_keywords - sql_keywords )
2833
34+ models = list (get_models (context , file_uri ))
2935 return AllModelsResponse (
30- models = list (get_models (context , file_uri )),
36+ models = [m .name for m in models ],
37+ model_completions = models ,
3138 keywords = all_keywords ,
3239 macros = list (get_macros (context , file_uri )),
3340 )
3441
3542
36- def get_models (context : t .Optional [LSPContext ], file_uri : t .Optional [URI ]) -> t .Set [str ]:
43+ def get_models (
44+ context : t .Optional [LSPContext ], file_uri : t .Optional [URI ]
45+ ) -> t .List [ModelCompletion ]:
3746 """
3847 Return a list of models for a given file.
3948
4049 If there is no context, return an empty list.
4150 If there is a context, return a list of all models bar the ones the file itself defines.
4251 """
4352 if context is None :
44- return set ()
53+ return []
54+
55+ current_path = file_uri .to_path () if file_uri is not None else None
56+
57+ completions : t .List [ModelCompletion ] = []
58+ for model in context .context .models .values ():
59+ if current_path is not None and model ._path == current_path :
60+ continue
61+ description = None
62+ try :
63+ description = generate_markdown_description (model )
64+ except Exception :
65+ description = getattr (model , "description" , None )
66+
67+ completions .append (ModelCompletion (name = model .name , description = description ))
4568
46- all_models = set ()
47- # Extract model names from ModelInfo objects
48- for file_info in context .map .values ():
49- if isinstance (file_info , ModelTarget ):
50- all_models .update (file_info .names )
51-
52- # Remove models from the current file
53- path = file_uri .to_path () if file_uri is not None else None
54- if path is not None and path in context .map :
55- file_info = context .map [path ]
56- if isinstance (file_info , ModelTarget ):
57- for model in file_info .names :
58- all_models .discard (model )
59-
60- return all_models
69+ return completions
6170
6271
6372def get_macros (
0 commit comments