1414//! running program; then it removes every installed version, because the plugin page shows one
1515//! row per plugin and an older version left behind would keep the plugin installed; then it
1616//! forgets the choice and removes the state the programs wrote, so a later installation starts
17- //! as a first one. Fixtures live in `rust/fixtures/plugins/installed-lifecycle.json`.
17+ //! as a first one.
18+ //!
19+ //! A language has one language server: two plugins that both serve it would race for every
20+ //! document of it, and neither would know the other answers too. So enabling a plugin, or
21+ //! confirming its installation while it is enabled, turns off every other enabled plugin whose
22+ //! newest installed version serves one of the same languages, and the host records that and
23+ //! stops their programs. Choices made before this rule can still enable two such plugins;
24+ //! enablement then keeps the first by ID and reports the others disabled, without changing the
25+ //! choices, so enabling one of them later is what settles it. Fixtures live in
26+ //! `rust/fixtures/plugins/installed-lifecycle.json`.
1827
1928use std:: collections:: { BTreeMap , BTreeSet } ;
2029
30+ use lithe_plugin_model:: PluginVersion ;
2131use lithe_protocol:: { CoreError , ErrorCode } ;
2232use serde:: { Deserialize , Serialize } ;
2333use serde_json:: Value ;
2434
2535/// Upper bound of installed versions and of choices in one request.
2636const MAX_ENTRIES : usize = 4096 ;
2737
38+ /// Upper bound of the languages one installed version serves; a manifest declares at most one
39+ /// language server per language it declares.
40+ const MAX_LANGUAGE_SERVERS : usize = 256 ;
41+
42+ /// Upper bound of one language ID, in bytes.
43+ const MAX_LANGUAGE_ID_BYTES : usize = 256 ;
44+
2845/// Rejection of an action on a plugin that has no installed version: enabling it would
2946/// silently enable a later installation, and there is nothing to uninstall.
3047pub const NOT_INSTALLED : & str = "plugin.lifecycle.notInstalled" ;
@@ -48,6 +65,10 @@ struct InstalledVersion {
4865 #[ serde( rename = "pluginID" ) ]
4966 plugin_id : String ,
5067 version : String ,
68+ /// The `languageID` of every language server this version's manifest declares; only the
69+ /// plugin's newest installed version counts, since it is the one that runs.
70+ #[ serde( default , rename = "languageServers" ) ]
71+ language_servers : Vec < String > ,
5172}
5273
5374/// One plugin and whether it is enabled: a choice in the request, a plugin's enablement in the
@@ -96,6 +117,15 @@ enum Step {
96117 RemoveVersion { version : String } ,
97118 /// Remove the state directory the plugin's programs wrote.
98119 RemoveState ,
120+ /// Turn another plugin off because the action's plugin now serves `language_ids` too:
121+ /// record the choice `enabled: false` for `plugin_id`, then stop its programs. Unlike the
122+ /// other steps it is on `plugin_id`, not on the action's plugin.
123+ DisplacePlugin {
124+ #[ serde( rename = "pluginID" ) ]
125+ plugin_id : String ,
126+ #[ serde( rename = "languageIDs" ) ]
127+ language_ids : Vec < String > ,
128+ } ,
99129}
100130
101131#[ derive( Debug , Serialize ) ]
@@ -135,6 +165,7 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
135165 if entry. plugin_id . is_empty ( ) || entry. version . is_empty ( ) {
136166 return Err ( invalid ( "An installed plugin has an empty ID or version" ) ) ;
137167 }
168+ validate_language_servers ( & entry. language_servers ) ?;
138169 if !versions
139170 . entry ( & entry. plugin_id )
140171 . or_default ( )
@@ -152,9 +183,10 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
152183 return Err ( invalid ( "A plugin has two choices" ) ) ;
153184 }
154185 }
186+ let served = served_languages ( & request. installed ) ;
155187 let Some ( action) = & request. action else {
156188 return Ok ( encode ( Response {
157- plugins : enablement ( & versions, & choices, None ) ,
189+ plugins : enablement ( & versions, & choices, & served , None ) ,
158190 changed : false ,
159191 steps : Vec :: new ( ) ,
160192 rejection : None ,
@@ -168,7 +200,7 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
168200 let id = action. plugin_id . as_str ( ) ;
169201 let Some ( installed) = versions. get ( id) else {
170202 return Ok ( encode ( Response {
171- plugins : enablement ( & versions, & choices, None ) ,
203+ plugins : enablement ( & versions, & choices, & served , None ) ,
172204 changed : false ,
173205 steps : Vec :: new ( ) ,
174206 rejection : Some ( NOT_INSTALLED ) ,
@@ -214,30 +246,124 @@ pub fn command(payload: Value) -> Result<Value, CoreError> {
214246 if let Some ( enabled) = after {
215247 choices. insert ( id, enabled) ;
216248 }
249+ if after == Some ( true ) {
250+ steps. extend ( displace_rivals ( id, & served, & mut choices) ) ;
251+ }
217252 Ok ( encode ( Response {
218- plugins : enablement ( & versions, & choices, uninstalled) ,
253+ plugins : enablement ( & versions, & choices, & served , uninstalled) ,
219254 changed : after != chosen,
220255 steps,
221256 rejection : None ,
222257 } ) )
223258}
224259
225- /// Every installed plugin but `left_out`, by ID, enabled only when its choice says so.
260+ /// Every installed plugin but `left_out`, by ID, enabled only when its choice says so and no
261+ /// plugin before it by ID that is enabled already serves one of its languages.
226262fn enablement (
227263 versions : & BTreeMap < & str , BTreeSet < & str > > ,
228264 choices : & BTreeMap < & str , bool > ,
265+ served : & BTreeMap < & str , BTreeSet < & str > > ,
229266 left_out : Option < & str > ,
230267) -> Vec < Choice > {
268+ let none = BTreeSet :: new ( ) ;
269+ let mut claimed: BTreeSet < & str > = BTreeSet :: new ( ) ;
231270 versions
232271 . keys ( )
233272 . filter ( |id| Some ( * * id) != left_out)
234- . map ( |id| Choice {
235- plugin_id : ( * id) . to_string ( ) ,
236- enabled : choices. get ( id) . copied ( ) . unwrap_or ( false ) ,
273+ . map ( |id| {
274+ let languages = served. get ( id) . unwrap_or ( & none) ;
275+ let enabled =
276+ choices. get ( id) . copied ( ) . unwrap_or ( false ) && languages. is_disjoint ( & claimed) ;
277+ if enabled {
278+ claimed. extend ( languages. iter ( ) . copied ( ) ) ;
279+ }
280+ Choice {
281+ plugin_id : ( * id) . to_string ( ) ,
282+ enabled,
283+ }
284+ } )
285+ . collect ( )
286+ }
287+
288+ /// The languages each installed plugin's newest version serves.
289+ ///
290+ /// Versions are compared as `major.minor.patch`; a version that is not one ranks below every
291+ /// one that is, and of two such versions the first listed counts.
292+ fn served_languages ( installed : & [ InstalledVersion ] ) -> BTreeMap < & str , BTreeSet < & str > > {
293+ let mut newest: BTreeMap < & str , ( Option < PluginVersion > , & InstalledVersion ) > = BTreeMap :: new ( ) ;
294+ for entry in installed {
295+ let version = PluginVersion :: parse ( & entry. version ) ;
296+ let current = newest. entry ( & entry. plugin_id ) . or_insert ( ( version, entry) ) ;
297+ if version > current. 0 {
298+ * current = ( version, entry) ;
299+ }
300+ }
301+ newest
302+ . into_iter ( )
303+ . map ( |( id, ( _, entry) ) | {
304+ (
305+ id,
306+ entry. language_servers . iter ( ) . map ( String :: as_str) . collect ( ) ,
307+ )
237308 } )
238309 . collect ( )
239310}
240311
312+ /// Turns off, in `choices`, every other enabled plugin that serves one of `id`'s languages, and
313+ /// returns the steps that make the host do the same, in plugin ID order.
314+ fn displace_rivals < ' a > (
315+ id : & str ,
316+ served : & BTreeMap < & ' a str , BTreeSet < & ' a str > > ,
317+ choices : & mut BTreeMap < & ' a str , bool > ,
318+ ) -> Vec < Step > {
319+ let Some ( languages) = served. get ( id) . filter ( |languages| !languages. is_empty ( ) ) else {
320+ return Vec :: new ( ) ;
321+ } ;
322+ let mut steps = Vec :: new ( ) ;
323+ for ( other, other_languages) in served {
324+ if * other == id || choices. get ( other) != Some ( & true ) {
325+ continue ;
326+ }
327+ let shared: Vec < String > = languages
328+ . intersection ( other_languages)
329+ . map ( |language| ( * language) . to_string ( ) )
330+ . collect ( ) ;
331+ if shared. is_empty ( ) {
332+ continue ;
333+ }
334+ choices. insert ( other, false ) ;
335+ steps. push ( Step :: DisplacePlugin {
336+ plugin_id : ( * other) . to_string ( ) ,
337+ language_ids : shared,
338+ } ) ;
339+ }
340+ steps
341+ }
342+
343+ /// Rejects a malformed `languageServers` list: too long, an empty or overlong ID, or an ID
344+ /// listed twice, which a valid manifest never produces.
345+ fn validate_language_servers ( languages : & [ String ] ) -> Result < ( ) , CoreError > {
346+ if languages. len ( ) > MAX_LANGUAGE_SERVERS {
347+ return Err ( invalid (
348+ "An installed plugin version serves too many languages" ,
349+ ) ) ;
350+ }
351+ let mut seen = BTreeSet :: new ( ) ;
352+ for language in languages {
353+ if language. is_empty ( ) || language. len ( ) > MAX_LANGUAGE_ID_BYTES {
354+ return Err ( invalid (
355+ "An installed plugin version serves an empty or overlong language ID" ,
356+ ) ) ;
357+ }
358+ if !seen. insert ( language. as_str ( ) ) {
359+ return Err ( invalid (
360+ "An installed plugin version serves a language twice" ,
361+ ) ) ;
362+ }
363+ }
364+ Ok ( ( ) )
365+ }
366+
241367fn invalid ( message : & str ) -> CoreError {
242368 CoreError :: new ( ErrorCode :: InvalidRequest , message)
243369}
0 commit comments