Skip to content

Commit

Permalink
Merge pull request #58 from Exafunction/rahul/intellisense-compatibility
Browse files Browse the repository at this point in the history
Make compatible with Intellisense; fix crashes
  • Loading branch information
fortenforge authored Mar 2, 2024
2 parents f87a044 + ac178ad commit 867b92d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
4 changes: 0 additions & 4 deletions CodeiumVS/SuggestionUI/SuggestionTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,6 @@ void AddInsertionTextBlock(int start, int end, string line)
if (line.Length <= suggestionIndex) return;

string remainder = line.Substring(start, end - start);

ITextSnapshotLine snapshotLine = view.TextSnapshot.GetLineFromLineNumber(currentTextLineN);
var lineFormat = view.TextViewLines.GetCharacterBounds(snapshotLine.Start);

var textBlock = CreateTextBox(remainder, greyBrush);
GetTagger().UpdateAdornment(textBlock);
}
Expand Down
39 changes: 32 additions & 7 deletions CodeiumVS/SuggestionUI/TextViewListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public async void GetCompletion()
await package.LogAsync(
$"RequestProposalsAsync - Language: {_language.Name}; Caret: {caretPosition}; ASCII: {_document.Encoding.IsSingleByte}");

string text = _view.TextSnapshot.GetText();
string text = _document.TextBuffer.CurrentSnapshot.GetText();
int cursorPosition = _document.Encoding.IsSingleByte
? caretPosition
: Utf16OffsetToUtf8Offset(text, caretPosition);
Expand Down Expand Up @@ -159,6 +159,20 @@ List<Tuple<String, String>> ParseCompletion(IList<Packets.CompletionItem> comple
completionText = completionText.Substring(offset);
string completionID = completionItem.completion.completionId;
var set = new Tuple<String, String>(completionText, completionID);

// Filter out completions that don't match the current intellisense prefix
ICompletionSession session = m_provider.CompletionBroker.GetSessions(_view).FirstOrDefault();
if (session != null && session.SelectedCompletionSet != null)
{
string intellisenseSuggestion = session.SelectedCompletionSet.SelectionStatus.Completion.InsertionText;
ITrackingSpan intellisenseSpan = session.SelectedCompletionSet.ApplicableTo;
SnapshotSpan span = intellisenseSpan.GetSpan(intellisenseSpan.TextBuffer.CurrentSnapshot);
string intellisenseInsertion = intellisenseSuggestion.Substring(span.Length);
if (!completionText.StartsWith(intellisenseInsertion))
{
continue;
}
}
list.Add(set);
}

Expand Down Expand Up @@ -220,7 +234,7 @@ internal CodeiumCompletionHandler(IVsTextView textViewAdapter, ITextView view,
_textViewAdapter = textViewAdapter;
// add the command to the command chain
textViewAdapter.AddCommandFilter(this, out m_nextCommandHandler);
ShowIntellicodeMsg();
// ShowIntellicodeMsg();
}

private void OnContentTypeChanged(object sender, ContentTypeChangedEventArgs e)
Expand Down Expand Up @@ -315,18 +329,29 @@ public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pv
}

// check for a commit character
bool regenerateSuggestion = false;
if (!hasCompletionUpdated && nCmdID == (uint)VSConstants.VSStd2KCmdID.TAB)
{

var tagger = GetTagger();

if (tagger != null)
{
if (tagger.IsSuggestionActive() && tagger.CompleteText())
if (tagger.IsSuggestionActive())
{
ClearCompletionSessions();
OnSuggestionAccepted(currentCompletionID);
return VSConstants.S_OK;
// If there is an active Intellisense session, let that one get accepted first.
ICompletionSession session = m_provider.CompletionBroker.GetSessions(_view).FirstOrDefault();
if (session != null && session.SelectedCompletionSet != null)
{
tagger.ClearSuggestion();
regenerateSuggestion = true;
}
if (tagger.CompleteText())
{
ClearCompletionSessions();
OnSuggestionAccepted(currentCompletionID);
return VSConstants.S_OK;
}
}
else { tagger.ClearSuggestion(); }
}
Expand Down Expand Up @@ -357,7 +382,7 @@ public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pv

if (hasCompletionUpdated) { ClearSuggestion(); }
// gets lsp completions on added character or deletions
if (!typedChar.Equals(char.MinValue) || commandID == (uint)VSConstants.VSStd2KCmdID.RETURN)
if (!typedChar.Equals(char.MinValue) || commandID == (uint)VSConstants.VSStd2KCmdID.RETURN || regenerateSuggestion)
{
_ = Task.Run(() => GetCompletion());
handled = true;
Expand Down
1 change: 1 addition & 0 deletions CodeiumVS/Windows/ChatToolWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ public async Task ReloadAsync()
{ "app_name", "Visual Studio" },
{ "web_server_url", serverUrl },
{ "has_dev_extension", "false" },
{ "has_index_service", "true" },
{ "open_file_pointer_enabled", "true" },
{ "diff_view_enabled", "true" },
{ "insert_at_cursor_enabled", "true" },
Expand Down

0 comments on commit 867b92d

Please sign in to comment.