Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed rpc issue #22

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
c4dfe37
adding files for refact extension
Oct 27, 2023
64a5f05
testing github workflow
Oct 30, 2023
91f6717
removing default exe to test workflow
Oct 30, 2023
8dbd51e
uploading artifacts in workflow
Oct 30, 2023
1495fe4
readding exe
Oct 30, 2023
567dba0
cleaning up files
Nov 1, 2023
afd128e
C/C++ now works with grey text
Nov 2, 2023
faa87c6
fixed minor typo
Nov 2, 2023
37e9e90
can now select intellisense options, whitespace causes less problems …
Nov 7, 2023
c477f10
removing binary and updating build scripts
Nov 8, 2023
3153d7a
fixing build script typo
Nov 8, 2023
becfe12
fixing minor build naming issue
Nov 8, 2023
2cfa582
Updating code to be consistent with new build script
Nov 8, 2023
28de23e
improved the options page
Nov 8, 2023
d35bd9c
added comments and fixed formatting
Nov 13, 2023
8b50fb9
updating enter behaviour
Nov 17, 2023
b5e9ea3
fixing minor bugs and functional issues
Nov 21, 2023
ff548ad
added code to fix issues around single vs multiline completions issue…
Nov 30, 2023
341cc2b
added scroll bars for the options page
Nov 30, 2023
f965f5c
adding status bar and killing server process on shutdown
Dec 5, 2023
fc9ba10
fixing minor bug where grey text was inserted into incorrect places
Dec 5, 2023
6421422
Merge remote-tracking branch 'upstream/main'
Dec 5, 2023
d36ac11
Merge branch 'smallcloudai:main' into main
digital-phoenix Dec 27, 2023
33a706c
Update RefactLanguageClient.cs
digital-phoenix Dec 27, 2023
da10582
resolved potential issues
Dec 27, 2023
7254221
Update RefactLanguageClient.cs
digital-phoenix Dec 29, 2023
6b8e02f
Update RefactLanguageClient.cs
digital-phoenix Dec 29, 2023
b8f550d
Update RefactLanguageClient.cs
digital-phoenix Dec 29, 2023
04bbc3a
fixed issues for issue #19
Jan 4, 2024
3862160
Update RefactLanguageClient.cs
digital-phoenix Jan 4, 2024
9d181d7
Merge branch 'main' into main
digital-phoenix Jan 10, 2024
bda9075
fixed all the bugs
digital-phoenix Jan 17, 2024
f375070
fixed spacing issue
digital-phoenix Jan 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading