Skip to content

Commit

Permalink
Merge branch 'candidate'
Browse files Browse the repository at this point in the history
  • Loading branch information
reymondzzzz committed Jan 24, 2024
2 parents 97ead1a + 8212260 commit a2f22ad
Show file tree
Hide file tree
Showing 6 changed files with 460 additions and 431 deletions.
13 changes: 11 additions & 2 deletions MultilineGreyText/InlineGreyTextTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,20 @@ public InlineGreyTextTagger(IWpfTextView view){
/// <returns>Adornment corresponding to given data. May be null.</returns>
public void UpdateAdornment(UIElement text){
ClearAdornment();
stackPanel.Children.Add(text);
stackPanel.Children.Add(text);
stackPanel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
stackPanel.UpdateLayout();
}

public void ClearAdornment(){
stackPanel.Children.Clear();
stackPanel = new StackPanel();
}

public void FormatText(TextRunProperties props){
if(props == null){
return;
}
foreach (TextBlock block in stackPanel.Children){
block.FontFamily = props.Typeface.FontFamily;
block.FontSize = props.FontRenderingEmSize;
Expand All @@ -68,7 +74,10 @@ public virtual IEnumerable<ITagSpan<IntraTextAdornmentTag>> GetTags(NormalizedSn
}

ITextSnapshot requestedSnapshot = spans[0].Snapshot;
stackPanel.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double width = view.FormattedLineSource.ColumnWidth * ((stackPanel.Children[0] as TextBlock).Inlines.First() as Run).Text.Length;
stackPanel.Measure(new Size(width, double.PositiveInfinity));
stackPanel.MinWidth = width;
stackPanel.MaxWidth = width;
var caretLine = view.Caret.ContainingTextViewLine;
SnapshotPoint point = view.Caret.Position.BufferPosition.TranslateTo(requestedSnapshot, PointTrackingMode.Positive);
var line = requestedSnapshot.GetLineFromPosition(point);
Expand Down
83 changes: 43 additions & 40 deletions MultilineGreyText/MultilineGreyTextTagger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,39 +70,39 @@ private InlineGreyTextTagger GetTagger(){
}

public void SetSuggestion(String newSuggestion, bool inline, int caretPoint){
ClearSuggestion();
inlineSuggestion = inline;

int lineN = GetCurrentTextLine();

if (lineN < 0) return;

String untrim = buffer.CurrentSnapshot.GetLineFromLineNumber(lineN).GetText();
String line = untrim.TrimStart();
int offset = untrim.Length - line.Length;

caretPoint = Math.Max(0, caretPoint - offset);
String combineSuggestion = line + newSuggestion;
if (line.Length - caretPoint > 0){
String currentText = line.Substring(0, caretPoint);
combineSuggestion = currentText + newSuggestion;
userEndingText = line.TrimEnd().Substring(caretPoint);
var userIndex = newSuggestion.IndexOf(userEndingText);
if(userIndex < 0){
return;
}
userIndex += currentText.Length;

this.userIndex = userIndex;
isTextInsertion = true;
insertionPoint = line.Length - caretPoint;
}else{
isTextInsertion = false;
}

suggestion = new Tuple<String, String[]>(combineSuggestion, combineSuggestion.Split('\n'));
Update();
ClearSuggestion();
inlineSuggestion = inline;

int lineN = GetCurrentTextLine();

if (lineN < 0) return;

String untrim = buffer.CurrentSnapshot.GetLineFromLineNumber(lineN).GetText();
String line = untrim.TrimStart();
int offset = untrim.Length - line.Length;

caretPoint = Math.Max(0, caretPoint - offset);

String combineSuggestion = line + newSuggestion;
if (line.Length - caretPoint > 0){
String currentText = line.Substring(0, caretPoint);
combineSuggestion = currentText + newSuggestion;
userEndingText = line.Substring(caretPoint).TrimEnd();
var userIndex = newSuggestion.IndexOf(userEndingText);
if (userIndex < 0){
return;
}
userIndex += currentText.Length;

this.userIndex = userIndex;
isTextInsertion = true;
insertionPoint = line.Length - caretPoint;
}else{
isTextInsertion = false;
}

suggestion = new Tuple<String, String[]>(combineSuggestion, combineSuggestion.Split('\n'));
Update();
}

private void CaretUpdate(object sender, CaretPositionChangedEventArgs e){
Expand Down Expand Up @@ -163,7 +163,7 @@ public IEnumerable<ITagSpan<TestTag>> GetTags(NormalizedSnapshotSpanCollection s
var snapshotLine = currentSnapshot.GetLineFromLineNumber(currentTextLineN);

var height = view.LineHeight * (currentSuggestion.Item2.Length - 1);

if(currentTextLineN == 0 && currentSnapshot.Lines.Count() == 1 && String.IsNullOrEmpty(currentSnapshot.GetText())){
height += view.LineHeight;
}
Expand Down Expand Up @@ -241,7 +241,6 @@ void AddInsertionTextBlock(int start, int end, string line){
GetTagger().UpdateAdornment(CreateTextBox(remainder, greyBrush));
}


//Updates the grey text
public void UpdateAdornment(IWpfTextView view, string userText, int suggestionStart){
stackPanel.Children.Clear();
Expand Down Expand Up @@ -456,12 +455,16 @@ public bool CompleteText(){
//replaces text in the editor
void ReplaceText(string text, int lineN){
ClearSuggestion();

SnapshotSpan span = this.snapshot.GetLineFromLineNumber(lineN).Extent;
ITextEdit edit = view.TextBuffer.CreateEdit();

edit.Replace(span, text);
edit.Apply();
ITextEdit edit = view.BufferGraph.TopBuffer.CreateEdit();
var spanLength = span.Length;
edit.Replace(span, text);
var newSnapshot = edit.Apply();

if(spanLength == 0 && text.Length > 0){
view.Caret.MoveToPreviousCaretPosition();
view.Caret.MoveToNextCaretPosition();
}
}

//sets up the suggestion for display
Expand Down
77 changes: 30 additions & 47 deletions MultilineGreyText/RefactCompletionCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public LanguageClientMetadata(string[] contentTypes, string clientName = null){
private int version = 0;

private bool hasCompletionUpdated = false;
private Task<string> completionTask = null;

//The command Handler processes keyboard input.
internal RefactCompletionCommandHandler(IVsTextView textViewAdapter, ITextView textView, RefactCompletionHandlerProvider provider){
Expand Down Expand Up @@ -79,12 +80,22 @@ void LoadLsp(String file, ITextDocument doc){
}

//Adds file to LSP
void ConnectFileToLSP(){
async Task ConnectFileToLSP(){
if (!client.ContainsFile(filePath)){
client.AddFile(filePath, doc.TextBuffer.CurrentSnapshot.GetText());

//listen for changes
((ITextBuffer2)doc.TextBuffer).ChangedHighPriority += ChangeEvent;
await client.AddFile(filePath, doc.TextBuffer.CurrentSnapshot.GetText());
}else{
version++;
TextDocumentContentChangeEvent[] contentChanges = new TextDocumentContentChangeEvent[1];
var snapshot = doc.TextBuffer.CurrentSnapshot;
contentChanges[0] = new TextDocumentContentChangeEvent {
Text = snapshot.GetText(),
Range = new Range {
Start = new Position(0, 0),
End = new Position(snapshot.Lines.Count(), 0)
},
RangeLength = snapshot.Lines.Count()
};
await this.client.InvokeTextDocumentDidChangeAsync(fileURI, version, contentChanges);
}
}

Expand All @@ -98,34 +109,6 @@ private MultilineGreyTextTagger GetTagger(){
}
}

//Send changes to LSP
private void ChangeEvent(object sender, TextContentChangedEventArgs args){
version++;

//converts the changelist to be readable by LSP
TextDocumentContentChangeEvent[] contentChanges = args.Changes.Reverse().Select<ITextChange, TextDocumentContentChangeEvent>(change => {
int startLine, startColumn;
textViewAdapter.GetLineAndColumn(change.OldSpan.Start, out startLine, out startColumn);
int endLine, endColumn;
textViewAdapter.GetLineAndColumn(change.OldSpan.End, out endLine, out endColumn);

return new TextDocumentContentChangeEvent{
Text = change.NewText,
Range = new Range{
Start = new Position(startLine, startColumn),
End = new Position(endLine, endColumn)
},
RangeLength = change.OldSpan.Length
};
}).ToArray();

//sends changes to LSP
if (contentChanges.Length > 0){
contentChanges[0].Text = m_textView.TextBuffer.CurrentSnapshot.GetText();
this.client.InvokeTextDocumentDidChangeAsync(fileURI, version, contentChanges);
}
}

//required by interface just boiler plate
public int QueryStatus(ref Guid pguidCmdGroup, uint cCmds, OLECMD[] prgCmds, IntPtr pCmdText){
return m_nextCommandHandler.QueryStatus(ref pguidCmdGroup, cCmds, prgCmds, pCmdText);
Expand All @@ -137,7 +120,7 @@ public bool IsInline(int lineN){
}

//gets recommendations from LSP
public void GetLSPCompletions(){
public async void GetLSPCompletions(){
if (!General.Instance.PauseCompletion){
SnapshotPoint? caretPoint = m_textView.Caret.Position.Point.GetPoint(textBuffer => (!textBuffer.ContentType.IsOfType("projection")), PositionAffinity.Predecessor);

Expand All @@ -159,27 +142,27 @@ public void GetLSPCompletions(){
return;
}

if (!client.ContainsFile(filePath)){
ConnectFileToLSP();
}
await ConnectFileToLSP();

hasCompletionUpdated = false;
bool multiline = !IsInline(lineN);
var refactRes = client.RefactCompletion(m_textView.TextBuffer.Properties, filePath, lineN, multiline ? 0 : characterN, multiline);
ShowRefactSuggestion(refactRes, new Tuple<int, int>(lineN, characterN));
if(completionTask == null || completionTask.IsCompleted){
completionTask = client.RefactCompletion(m_textView.TextBuffer.Properties, filePath, lineN, multiline ? 0 : characterN, multiline);
var s = await completionTask;
await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();
if (completionTask == null || completionTask.IsCompleted){
ShowRefactSuggestion(s, lineN, characterN);
}
}
}
}
}
}

//sends lsp reccomendations to grey text tagger to be dispalyed
public async void ShowRefactSuggestion(Task<string> res, Object position){
var p = position as Tuple<int, int>;
int lineN = p.Item1;
int characterN = p.Item2;
public void ShowRefactSuggestion(String s, int lineN, int characterN){

String s = await res;
if (res != null){
if (!string.IsNullOrEmpty(s)){
//the caret must be in a non-projection location
SnapshotPoint? caretPoint = m_textView.Caret.Position.Point.GetPoint(textBuffer => (!textBuffer.ContentType.IsOfType("projection")), PositionAffinity.Predecessor);
if (!caretPoint.HasValue){
Expand Down Expand Up @@ -265,10 +248,10 @@ public int Exec(ref Guid pguidCmdGroup, uint nCmdID, uint nCmdexecopt, IntPtr pv

//gets lsp completions on added character or deletions
if (!typedChar.Equals(char.MinValue) || commandID == (uint)VSConstants.VSStd2KCmdID.RETURN){
GetLSPCompletions();
_ = Task.Run(() => GetLSPCompletions());
handled = true;
}else if (commandID == (uint)VSConstants.VSStd2KCmdID.BACKSPACE || commandID == (uint)VSConstants.VSStd2KCmdID.DELETE){
GetLSPCompletions();
_ = Task.Run(()=>GetLSPCompletions());
handled = true;
}

Expand Down
Loading

0 comments on commit a2f22ad

Please sign in to comment.