Skip to content

Commit

Permalink
Centralise logic for handling of completion-trigger characters
Browse files Browse the repository at this point in the history
  • Loading branch information
tintoy committed Jul 2, 2020
1 parent e89be93 commit e938202
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,35 @@ protected CompletionProvider(ILogger logger)
/// The sort text.
/// </returns>
protected virtual string GetItemSortText(string completionLabel, int? priority = null) => $"{priority ?? Priority:0000}{completionLabel}";

/// <summary>
/// Handle characters (if any) that triggered the completion.
/// </summary>
/// <param name="projectDocument">
/// The <see cref="ProjectDocument"/> that contains the <paramref name="location"/>.
/// </param>
/// <param name="triggerCharacters">
/// The character(s), if any, that triggered completion.
/// </param>
/// <param name="targetRange">
/// The target <see cref="Range"/> for completions.
/// </param>
/// <returns>
/// <c>true</c>, if any trigger characters were handled (i.e. the selection was extended); otherwise, <c>false</c>.
/// </returns>
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,7 @@ IEnumerable<CompletionItem> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompletionItem> elementCompletions = HandlePackageReferenceElementCompletion(location, projectDocument, targetRange);
if (elementCompletions != null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e938202

Please sign in to comment.