diff --git a/src/LanguageServer.Engine/CompletionProviders/CommentCompletion.cs b/src/LanguageServer.Engine/CompletionProviders/CommentCompletion.cs index 9c2421b..e91b9b4 100644 --- a/src/LanguageServer.Engine/CompletionProviders/CommentCompletion.cs +++ b/src/LanguageServer.Engine/CompletionProviders/CommentCompletion.cs @@ -93,12 +93,7 @@ public CommentCompletion(ILogger logger) } // Replace any characters that were typed to trigger the completion. - if (triggerCharacters != null) - { - targetRange = projectDocument.XmlPositions.ExtendLeft(targetRange, byCharCount: triggerCharacters.Length); - - Log.Verbose("Completion was triggered by typing one or more characters; target range will be extended by {TriggerCharacterCount} characters toward start of document (now: {TargetRange}).", triggerCharacters.Length, targetRange); - } + HandleTriggerCharacters(triggerCharacters, projectDocument, ref targetRange); completions.AddRange( GetCompletionItems(targetRange) diff --git a/src/LanguageServer.Engine/CompletionProviders/CompletionProvider.cs b/src/LanguageServer.Engine/CompletionProviders/CompletionProvider.cs index dc06fc0..ab70d14 100644 --- a/src/LanguageServer.Engine/CompletionProviders/CompletionProvider.cs +++ b/src/LanguageServer.Engine/CompletionProviders/CompletionProvider.cs @@ -79,5 +79,35 @@ protected CompletionProvider(ILogger logger) /// The sort text. /// protected virtual string GetItemSortText(string completionLabel, int? priority = null) => $"{priority ?? Priority:0000}{completionLabel}"; + + /// + /// Handle characters (if any) that triggered the completion. + /// + /// + /// The that contains the . + /// + /// + /// The character(s), if any, that triggered completion. + /// + /// + /// The target for completions. + /// + /// + /// true, if any trigger characters were handled (i.e. the selection was extended); otherwise, false. + /// + protected virtual bool HandleTriggerCharacters(string triggerCharacters, ProjectDocument projectDocument, ref Range targetRange) + { + // Replace any characters that were typed to trigger the completion. + if (triggerCharacters != null) + { + targetRange = projectDocument.XmlPositions.ExtendLeft(targetRange, byCharCount: triggerCharacters.Length); + + Log.Verbose("Completion was triggered by typing one or more characters; target range will be extended by {TriggerCharacterCount} characters toward start of document (now: {TargetRange}).", triggerCharacters.Length, targetRange); + + return true; + } + + return false; + } } } diff --git a/src/LanguageServer.Engine/CompletionProviders/ItemElementCompletion.cs b/src/LanguageServer.Engine/CompletionProviders/ItemElementCompletion.cs index a2a4036..d7b10da 100644 --- a/src/LanguageServer.Engine/CompletionProviders/ItemElementCompletion.cs +++ b/src/LanguageServer.Engine/CompletionProviders/ItemElementCompletion.cs @@ -102,12 +102,7 @@ public ItemElementCompletion(ILogger logger) } // Replace any characters that were typed to trigger the completion. - if (triggerCharacters != null) - { - targetRange = projectDocument.XmlPositions.ExtendLeft(targetRange, byCharCount: triggerCharacters.Length); - - Log.Verbose("Completion was triggered by typing one or more characters; target range will be extended by {TriggerCharacterCount} characters toward start of document (now: {TargetRange}).", triggerCharacters.Length, targetRange); - } + HandleTriggerCharacters(triggerCharacters, projectDocument, ref targetRange); completions.AddRange( GetCompletionItems(projectDocument, targetRange) diff --git a/src/LanguageServer.Engine/CompletionProviders/ItemMetadataCompletion.cs b/src/LanguageServer.Engine/CompletionProviders/ItemMetadataCompletion.cs index b36383a..c0edb28 100644 --- a/src/LanguageServer.Engine/CompletionProviders/ItemMetadataCompletion.cs +++ b/src/LanguageServer.Engine/CompletionProviders/ItemMetadataCompletion.cs @@ -232,12 +232,7 @@ IEnumerable GetElementCompletions(XmlLocation location, ProjectD } // Replace any characters that were typed to trigger the completion. - if (triggerCharacters != null) - { - targetRange = projectDocument.XmlPositions.ExtendLeft(targetRange, byCharCount: triggerCharacters.Length); - - Log.Verbose("Completion was triggered by typing one or more characters; target range will be extended by {TriggerCharacterCount} characters toward start of document (now: {TargetRange}).", triggerCharacters.Length, targetRange); - } + HandleTriggerCharacters(triggerCharacters, projectDocument, ref targetRange); // These items are handled by PackageReferenceCompletion. if (itemType == "PackageReference" || itemType == "DotNetCliToolReference") diff --git a/src/LanguageServer.Engine/CompletionProviders/PackageReferenceCompletion.cs b/src/LanguageServer.Engine/CompletionProviders/PackageReferenceCompletion.cs index de4eb3d..a0ac024 100644 --- a/src/LanguageServer.Engine/CompletionProviders/PackageReferenceCompletion.cs +++ b/src/LanguageServer.Engine/CompletionProviders/PackageReferenceCompletion.cs @@ -124,12 +124,7 @@ public PackageReferenceCompletion(ILogger logger) } // Replace any characters that were typed to trigger the completion. - if (triggerCharacters != null) - { - targetRange = projectDocument.XmlPositions.ExtendLeft(targetRange, byCharCount: triggerCharacters.Length); - - Log.Verbose("Completion was triggered by typing one or more characters; target range will be extended by {TriggerCharacterCount} characters toward start of document (now: {TargetRange}).", triggerCharacters.Length, targetRange); - } + HandleTriggerCharacters(triggerCharacters, projectDocument, ref targetRange); List elementCompletions = HandlePackageReferenceElementCompletion(location, projectDocument, targetRange); if (elementCompletions != null) diff --git a/src/LanguageServer.Engine/CompletionProviders/PropertyElementCompletion.cs b/src/LanguageServer.Engine/CompletionProviders/PropertyElementCompletion.cs index 907417a..03e2b54 100644 --- a/src/LanguageServer.Engine/CompletionProviders/PropertyElementCompletion.cs +++ b/src/LanguageServer.Engine/CompletionProviders/PropertyElementCompletion.cs @@ -98,12 +98,7 @@ public PropertyElementCompletion(ILogger logger) } // Replace any characters that were typed to trigger the completion. - if (triggerCharacters != null) - { - targetRange = projectDocument.XmlPositions.ExtendLeft(targetRange, byCharCount: triggerCharacters.Length); - - Log.Verbose("Completion was triggered by typing one or more characters; target range will be extended by {TriggerCharacterCount} characters toward start of document (now: {TargetRange}).", triggerCharacters.Length, targetRange); - } + HandleTriggerCharacters(triggerCharacters, projectDocument, ref targetRange); completions.AddRange( GetCompletionItems(projectDocument, targetRange) diff --git a/src/LanguageServer.Engine/CompletionProviders/TaskElementCompletion.cs b/src/LanguageServer.Engine/CompletionProviders/TaskElementCompletion.cs index 89defbf..6a3689c 100644 --- a/src/LanguageServer.Engine/CompletionProviders/TaskElementCompletion.cs +++ b/src/LanguageServer.Engine/CompletionProviders/TaskElementCompletion.cs @@ -104,12 +104,7 @@ public TaskElementCompletion(ILogger logger) Range targetRange = replaceElement.Range; // Replace any characters that were typed to trigger the completion. - if (triggerCharacters != null) - { - targetRange = projectDocument.XmlPositions.ExtendLeft(targetRange, byCharCount: triggerCharacters.Length); - - Log.Verbose("Completion was triggered by typing one or more characters; target range will be extended by {TriggerCharacterCount} characters toward start of document (now: {TargetRange}).", triggerCharacters.Length, targetRange); - } + HandleTriggerCharacters(triggerCharacters, projectDocument, ref targetRange); Log.Verbose("Offering completions to replace element {ElementName} @ {ReplaceRange:l}", replaceElement.Name, diff --git a/src/LanguageServer.Engine/CompletionProviders/TopLevelElementCompletion.cs b/src/LanguageServer.Engine/CompletionProviders/TopLevelElementCompletion.cs index 7dac12b..7a7be8a 100644 --- a/src/LanguageServer.Engine/CompletionProviders/TopLevelElementCompletion.cs +++ b/src/LanguageServer.Engine/CompletionProviders/TopLevelElementCompletion.cs @@ -97,12 +97,7 @@ public TopLevelElementCompletion(ILogger logger) } // Replace any characters that were typed to trigger the completion. - if (triggerCharacters != null) - { - targetRange = projectDocument.XmlPositions.ExtendLeft(targetRange, byCharCount: triggerCharacters.Length); - - Log.Verbose("Completion was triggered by typing one or more characters; target range will be extended by {TriggerCharacterCount} characters toward start of document (now: {TargetRange}).", triggerCharacters.Length, targetRange); - } + HandleTriggerCharacters(triggerCharacters, projectDocument, ref targetRange); completions.AddRange( GetCompletionItems(targetRange)