File tree Expand file tree Collapse file tree
src/OmniSharp.Roslyn.CSharp/Services/Completion
tests/OmniSharp.Roslyn.CSharp.Tests Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -220,6 +220,15 @@ private static void GetCompletionInfo(
220220 }
221221
222222 changeSpan = updatedChange . Span ;
223+ // When inserting at the beginning or middle of a word, we want to only replace characters
224+ // up until the caret position, but not after. For example when typing at the beginning of a word
225+ // we only want to insert the completion before the rest of the word.
226+ // However, Roslyn returns the entire word as the span to replace, so we have to adjust it.
227+ if ( position < changeSpan . End )
228+ {
229+ changeSpan = new ( changeSpan . Start , length : position - changeSpan . Start ) ;
230+ }
231+
223232 ( insertText , insertTextFormat ) = getPossiblySnippetizedInsertText ( updatedChange , adjustedNewPosition ) ;
224233
225234 // If we're expecting there to be unimported types, put in an explicit sort text to put things already in scope first.
Original file line number Diff line number Diff line change @@ -2250,6 +2250,24 @@ public C()
22502250 }
22512251 }
22522252
2253+ [ Theory ]
2254+ [ InlineData ( "dummy.cs" ) ]
2255+ [ InlineData ( "dummy.csx" ) ]
2256+ public async Task ReplacesUpUntilCursorInMiddleOfWord ( string filename )
2257+ {
2258+ const string input = @"public class C1 {}
2259+ pub$$class" ;
2260+
2261+ var completions = await FindCompletionsAsync ( filename , input , SharedOmniSharpTestHost ) ;
2262+ Assert . All ( completions . Items , ( completion ) =>
2263+ {
2264+ Assert . Equal ( 0 , completion . TextEdit . StartColumn ) ;
2265+ Assert . Equal ( 1 , completion . TextEdit . StartLine ) ;
2266+ Assert . Equal ( 3 , completion . TextEdit . EndColumn ) ;
2267+ Assert . Equal ( 1 , completion . TextEdit . EndLine ) ;
2268+ } ) ;
2269+ }
2270+
22532271 private CompletionService GetCompletionService ( OmniSharpTestHost host )
22542272 => host . GetRequestHandler < CompletionService > ( EndpointName ) ;
22552273
You can’t perform that action at this time.
0 commit comments