11import { InlineCompletionList , InlineCompletionParams , InlineCompletionItem } from 'vscode-languageserver-protocol' ;
2+ import { Context } from '../context/Context' ;
23import { ContextManager } from '../context/ContextManager' ;
4+ import { DocumentManager } from '../document/DocumentManager' ;
35import { Closeable , Configurable , ServerComponents } from '../server/ServerComponents' ;
6+ import { RelationshipSchemaService } from '../services/RelationshipSchemaService' ;
47import {
58 CompletionSettings ,
69 DefaultSettings ,
@@ -9,8 +12,10 @@ import {
912 SettingsSubscription ,
1013} from '../settings/Settings' ;
1114import { LoggerFactory } from '../telemetry/LoggerFactory' ;
15+ import { InlineCompletionProvider } from './InlineCompletionProvider' ;
16+ import { RelatedResourcesInlineCompletionProvider } from './RelatedResourcesInlineCompletionProvider' ;
1217
13- export type InlineCompletionProviderType = 'ResourceBlock ' | 'PropertyBlock ' | 'TemplateSection' | 'AIGenerated ';
18+ export type InlineCompletionProviderType = 'RelatedResources ' | 'ResourceBlock ' | 'PropertyBlock ' ;
1419type ReturnType = InlineCompletionList | InlineCompletionItem [ ] | null | undefined ;
1520
1621export class InlineCompletionRouter implements Configurable , Closeable {
@@ -20,7 +25,10 @@ export class InlineCompletionRouter implements Configurable, Closeable {
2025 private editorSettingsSubscription ?: SettingsSubscription ;
2126 private readonly log = LoggerFactory . getLogger ( InlineCompletionRouter ) ;
2227
23- constructor ( private readonly contextManager : ContextManager ) { }
28+ constructor (
29+ private readonly contextManager : ContextManager ,
30+ private readonly inlineCompletionProviderMap : Map < InlineCompletionProviderType , InlineCompletionProvider > ,
31+ ) { }
2432
2533 getInlineCompletions ( params : InlineCompletionParams ) : Promise < ReturnType > | ReturnType {
2634 if ( ! this . completionSettings . enabled ) return ;
@@ -31,6 +39,31 @@ export class InlineCompletionRouter implements Configurable, Closeable {
3139 return ;
3240 }
3341
42+ this . log . debug (
43+ {
44+ position : params . position ,
45+ section : context . section ,
46+ propertyPath : context . propertyPath ,
47+ } ,
48+ 'Processing inline completion request' ,
49+ ) ;
50+
51+ // Check if we are authoring a new resource
52+ if ( this . isAuthoringNewResource ( context ) ) {
53+ const relatedResourcesProvider = this . inlineCompletionProviderMap . get ( 'RelatedResources' ) ;
54+ if ( relatedResourcesProvider ) {
55+ const result = relatedResourcesProvider . getlineCompletion ( context , params , this . editorSettings ) ;
56+
57+ if ( result instanceof Promise ) {
58+ return result . then ( ( items ) => {
59+ return { items } ;
60+ } ) ;
61+ } else if ( result && Array . isArray ( result ) ) {
62+ return { items : result } ;
63+ }
64+ }
65+ }
66+
3467 return ;
3568 }
3669
@@ -42,18 +75,14 @@ export class InlineCompletionRouter implements Configurable, Closeable {
4275 this . editorSettingsSubscription . unsubscribe ( ) ;
4376 }
4477
45- // Get initial settings
46- this . completionSettings = settingsManager . getCurrentSettings ( ) . completion ;
47- this . editorSettings = settingsManager . getCurrentSettings ( ) . editor ;
48-
4978 // Subscribe to completion settings changes
5079 this . settingsSubscription = settingsManager . subscribe ( 'completion' , ( newCompletionSettings ) => {
51- this . onCompletionSettingsChanged ( newCompletionSettings ) ;
80+ this . completionSettings = newCompletionSettings ;
5281 } ) ;
5382
5483 // Subscribe to editor settings changes
5584 this . editorSettingsSubscription = settingsManager . subscribe ( 'editor' , ( newEditorSettings ) => {
56- this . onEditorSettingsChanged ( newEditorSettings ) ;
85+ this . editorSettings = newEditorSettings ;
5786 } ) ;
5887 }
5988
@@ -68,15 +97,36 @@ export class InlineCompletionRouter implements Configurable, Closeable {
6897 }
6998 }
7099
71- private onCompletionSettingsChanged ( settings : CompletionSettings ) : void {
72- this . completionSettings = settings ;
73- }
74-
75- private onEditorSettingsChanged ( settings : EditorSettings ) : void {
76- this . editorSettings = settings ;
100+ private isAuthoringNewResource ( context : Context ) : boolean {
101+ // Only provide suggestions in Resources section when positioned for a new resource
102+ return (
103+ String ( context . section ) === 'Resources' &&
104+ ( context . propertyPath . length === 1 ||
105+ ( context . propertyPath . length === 2 &&
106+ context . hasLogicalId &&
107+ context . isKey ( ) &&
108+ ! context . atEntityKeyLevel ( ) ) )
109+ ) ;
77110 }
78111
79112 static create ( components : ServerComponents ) {
80- return new InlineCompletionRouter ( components . contextManager ) ;
113+ return new InlineCompletionRouter (
114+ components . contextManager ,
115+ createInlineCompletionProviders ( components . documentManager , RelationshipSchemaService . getInstance ( ) ) ,
116+ ) ;
81117 }
82118}
119+
120+ export function createInlineCompletionProviders (
121+ documentManager : DocumentManager ,
122+ relationshipSchemaService : RelationshipSchemaService ,
123+ ) : Map < InlineCompletionProviderType , InlineCompletionProvider > {
124+ const inlineCompletionProviderMap = new Map < InlineCompletionProviderType , InlineCompletionProvider > ( ) ;
125+
126+ inlineCompletionProviderMap . set (
127+ 'RelatedResources' ,
128+ new RelatedResourcesInlineCompletionProvider ( relationshipSchemaService , documentManager ) ,
129+ ) ;
130+
131+ return inlineCompletionProviderMap ;
132+ }
0 commit comments