Skip to content

Commit

Permalink
Don't extend selection when providing completions via trigger characters
Browse files Browse the repository at this point in the history
This is no longer needed due to a change in VS Code / LSP completion behaviour.

#71
  • Loading branch information
tintoy committed Oct 14, 2023
1 parent bea189b commit 59c8bb3
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
47 changes: 47 additions & 0 deletions src/LanguageServer.Common/Utilities/TextPositions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ public sealed class TextPositions
/// </summary>
readonly int[] _lineStartPositions;

/// <summary>
/// The total length of the text.
/// </summary>
readonly int _textLength;

/// <summary>
/// Create a new <see cref="TextPositions"/> for the specified text.
/// </summary>
Expand All @@ -28,6 +33,7 @@ public TextPositions(string text)
throw new ArgumentNullException(nameof(text));

_lineStartPositions = CalculateLineStartPositions(text);
_textLength = text.Length;
}

/// <summary>
Expand Down Expand Up @@ -177,6 +183,28 @@ public Position MoveLeft(Position position, int byCharCount)
return GetPosition(absolutePosition);
}

/// <summary>
/// Move the specified position closer to the end of the document by the specified number of characters.
/// </summary>
/// <param name="position">
/// The position.
/// </param>
/// <param name="byCharCount">
/// The number of characters.
/// </param>
/// <returns>
/// The updated position. If <paramref name="byCharCount"/> would take the position past the end of the document, this becomes (1,1).
/// </returns>
public Position MoveRight(Position position, int byCharCount)
{
int absolutePosition = GetAbsolutePosition(position);
absolutePosition += byCharCount;
if (absolutePosition >= _textLength)
absolutePosition = _textLength - 1;

return GetPosition(absolutePosition);
}

/// <summary>
/// Extend the specified range closer to the start of the document by the specified number of characters.
/// </summary>
Expand All @@ -196,6 +224,25 @@ public Range ExtendLeft(Range range, int byCharCount)
);
}

/// <summary>
/// Extend the specified range closer to the end of the document by the specified number of characters.
/// </summary>
/// <param name="range">
/// The range.
/// </param>
/// <param name="byCharCount">
/// The number of characters.
/// </param>
/// <returns>
/// The updated range. If <paramref name="byCharCount"/> would take the range end past the end of the document, this becomes (1,1).
/// </returns>
public Range ExtendRight(Range range, int byCharCount)
{
return range.WithEnd(
MoveRight(range.End, byCharCount)
);
}

/// <summary>
/// Calculate the start position for each line in the text.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,9 @@ protected virtual bool HandleTriggerCharacters(string triggerCharacters, Project
if (projectDocument == null)
throw new ArgumentNullException(nameof(projectDocument));

// Replace any characters that were typed to trigger the completion.
if (triggerCharacters != null)
if (!String.IsNullOrEmpty(triggerCharacters))
{
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);
// NOTE: VSCode / LSP no longer require the selection to be extended when providing completions triggered by typing trigger characters, but you can still perform any other special handling here (if required).

return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public override async Task<CompletionList> ProvideCompletionsAsync(XmlLocation l
/// </returns>
public IEnumerable<CompletionItem> GetCompletionItems(ProjectDocument projectDocument, Range replaceRange)
{
LspModels.Range replaceRangeLsp = replaceRange.ToLsp();
LspModels.Range completionRange = replaceRange.ToLsp();

HashSet<string> offeredPropertyNames = new HashSet<string>();

Expand All @@ -136,7 +136,7 @@ public IEnumerable<CompletionItem> GetCompletionItems(ProjectDocument projectDoc

var defaultValues = MSBuildSchemaHelp.DefaultsForProperty(wellKnownPropertyName);

yield return PropertyCompletionItem(wellKnownPropertyName, replaceRangeLsp,
yield return PropertyCompletionItem(wellKnownPropertyName, completionRange,
description: MSBuildSchemaHelp.ForProperty(wellKnownPropertyName),
defaultValues: defaultValues
);
Expand All @@ -160,7 +160,7 @@ public IEnumerable<CompletionItem> GetCompletionItems(ProjectDocument projectDoc
if (!offeredPropertyNames.Add(propertyName))
continue;

yield return PropertyCompletionItem(propertyName, replaceRangeLsp, otherPropertyPriority,
yield return PropertyCompletionItem(propertyName, completionRange, otherPropertyPriority,
description: $"I don't know anything about the '{propertyName}' property, but it's defined in this project (or a project that it imports); you can override its value by specifying it here."
);
}
Expand All @@ -172,7 +172,7 @@ public IEnumerable<CompletionItem> GetCompletionItems(ProjectDocument projectDoc
/// <param name="propertyName">
/// The MSBuild property name.
/// </param>
/// <param name="replaceRange">
/// <param name="completionRange">
/// The range of text that will be replaced by the completion.
/// </param>
/// <param name="priority">
Expand All @@ -189,7 +189,7 @@ public IEnumerable<CompletionItem> GetCompletionItems(ProjectDocument projectDoc
/// <returns>
/// The <see cref="CompletionItem"/>.
/// </returns>
CompletionItem PropertyCompletionItem(string propertyName, LspModels.Range replaceRange, int? priority = null, string description = null, IReadOnlyList<string> defaultValues = null)
CompletionItem PropertyCompletionItem(string propertyName, LspModels.Range completionRange, int? priority = null, string description = null, IReadOnlyList<string> defaultValues = null)
{
return new CompletionItem
{
Expand All @@ -201,7 +201,7 @@ CompletionItem PropertyCompletionItem(string propertyName, LspModels.Range repla
TextEdit = new TextEdit
{
NewText = GetCompletionText(propertyName, defaultValues),
Range = replaceRange
Range = completionRange
},
InsertTextFormat = InsertTextFormat.Snippet
};
Expand Down

0 comments on commit 59c8bb3

Please sign in to comment.