-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
6 changed files
with
216 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using Microsoft.VisualStudio; | ||
using Microsoft.VisualStudio.Text; | ||
using Microsoft.VisualStudio.Text.Formatting; | ||
using RestClient; | ||
|
||
namespace RestClientVS | ||
{ | ||
public class Commenting | ||
{ | ||
public static async Task InitializeAsync() | ||
{ | ||
// We need to manually intercept the commenting command, because language services swallow these commands. | ||
await VS.Commands.InterceptAsync(VSConstants.VSStd2KCmdID.COMMENT_BLOCK, () => Execute(Comment)); | ||
await VS.Commands.InterceptAsync(VSConstants.VSStd2KCmdID.UNCOMMENT_BLOCK, () => Execute(Uncomment)); | ||
} | ||
|
||
private static CommandProgression Execute(Action<DocumentView> action) | ||
{ | ||
return ThreadHelper.JoinableTaskFactory.Run(async () => | ||
{ | ||
DocumentView doc = await VS.Documents.GetActiveDocumentViewAsync(); | ||
|
||
if (doc?.TextBuffer != null && doc.TextBuffer.ContentType.IsOfType(LanguageFactory.LanguageName)) | ||
{ | ||
action(doc); | ||
return CommandProgression.Stop; | ||
} | ||
|
||
return CommandProgression.Continue; | ||
}); | ||
} | ||
|
||
private static void Comment(DocumentView doc) | ||
{ | ||
SnapshotSpan spans = doc.TextView.Selection.SelectedSpans.First(); | ||
Collection<ITextViewLine> lines = doc.TextView.TextViewLines.GetTextViewLinesIntersectingSpan(spans); | ||
|
||
foreach (ITextViewLine line in lines.Reverse()) | ||
{ | ||
doc.TextBuffer.Insert(line.Start.Position, Constants.CommentChar.ToString()); | ||
} | ||
} | ||
|
||
private static void Uncomment(DocumentView doc) | ||
{ | ||
SnapshotSpan spans = doc.TextView.Selection.SelectedSpans.First(); | ||
Collection<ITextViewLine> lines = doc.TextView.TextViewLines.GetTextViewLinesIntersectingSpan(spans); | ||
|
||
foreach (ITextViewLine line in lines.Reverse()) | ||
{ | ||
var span = Span.FromBounds(line.Start, line.End); | ||
var originalText = doc.TextBuffer.CurrentSnapshot.GetText(span).TrimStart(Constants.CommentChar); | ||
Span commentCharSpan = new(span.Start, span.Length - originalText.Length); | ||
|
||
doc.TextBuffer.Delete(commentCharSpan); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
using System.Collections; | ||
using System.Linq; | ||
using Microsoft.VisualStudio.Editor; | ||
using Microsoft.VisualStudio.Package; | ||
using Microsoft.VisualStudio.Text.Editor; | ||
using Microsoft.VisualStudio.TextManager.Interop; | ||
using RestClient; | ||
|
||
namespace RestClientVS | ||
{ | ||
internal class DropdownBars : TypeAndMemberDropdownBars, IDisposable | ||
{ | ||
private readonly LanguageService _languageService; | ||
private readonly IWpfTextView _textView; | ||
private readonly Document _document; | ||
private bool _disposed; | ||
private bool _bufferHasChanged; | ||
|
||
public DropdownBars(IVsTextView textView, LanguageService languageService) | ||
: base(languageService) | ||
{ | ||
_languageService = languageService; | ||
|
||
IVsEditorAdaptersFactoryService adapter = VS.GetMefService<IVsEditorAdaptersFactoryService>(); | ||
|
||
_textView = adapter.GetWpfTextView(textView); | ||
_textView.Caret.PositionChanged += CaretPositionChanged; | ||
|
||
_document = _textView.TextBuffer.GetRestDocument(); | ||
_document.Parsed += OnDocumentParsed; | ||
|
||
SynchronizeDropdowns(); | ||
} | ||
|
||
private void OnDocumentParsed(object sender, EventArgs e) | ||
{ | ||
_bufferHasChanged = true; | ||
SynchronizeDropdowns(); | ||
} | ||
|
||
private void CaretPositionChanged(object sender, CaretPositionChangedEventArgs e) => SynchronizeDropdowns(); | ||
|
||
private void SynchronizeDropdowns() | ||
{ | ||
if (_document.IsParsing) | ||
{ | ||
return; | ||
} | ||
|
||
_ = ThreadHelper.JoinableTaskFactory.StartOnIdle(() => | ||
{ | ||
if (!_document.IsParsing) | ||
{ | ||
_languageService.SynchronizeDropdowns(); | ||
} | ||
}, VsTaskRunContext.UIThreadIdlePriority); | ||
} | ||
|
||
public override bool OnSynchronizeDropdowns(LanguageService languageService, IVsTextView textView, int line, int col, ArrayList dropDownTypes, ArrayList dropDownMembers, ref int selectedType, ref int selectedMember) | ||
{ | ||
if (_bufferHasChanged || dropDownMembers.Count == 0) | ||
{ | ||
dropDownMembers.Clear(); | ||
|
||
_document.Items.OfType<Request>() | ||
.Select(entry => CreateDropDownMember(entry, textView)) | ||
.ToList() | ||
.ForEach(ddm => dropDownMembers.Add(ddm)); | ||
} | ||
|
||
if (dropDownTypes.Count == 0) | ||
{ | ||
var thisExt = $"{Vsix.Name} ({Vsix.Version})"; | ||
dropDownTypes.Add(new DropDownMember(thisExt, new TextSpan(), 126, DROPDOWNFONTATTR.FONTATTR_GRAY)); | ||
} | ||
|
||
DropDownMember currentDropDown = dropDownMembers | ||
.OfType<DropDownMember>() | ||
.Where(d => d.Span.iStartLine <= line) | ||
.LastOrDefault(); | ||
|
||
selectedMember = dropDownMembers.IndexOf(currentDropDown); | ||
selectedType = 0; | ||
_bufferHasChanged = false; | ||
|
||
return true; | ||
} | ||
|
||
private static DropDownMember CreateDropDownMember(Request request, IVsTextView textView) | ||
{ | ||
TextSpan textSpan = GetTextSpan(request, textView); | ||
var text = request.Method.Text + " " + request.Url.Text; | ||
return new DropDownMember(text, textSpan, 126, DROPDOWNFONTATTR.FONTATTR_PLAIN); | ||
} | ||
|
||
private static TextSpan GetTextSpan(Request item, IVsTextView textView) | ||
{ | ||
TextSpan textSpan = new(); | ||
|
||
textView.GetLineAndColumn(item.Method.Start, out textSpan.iStartLine, out textSpan.iStartIndex); | ||
textView.GetLineAndColumn(item.Url.End + 1, out textSpan.iEndLine, out textSpan.iEndIndex); | ||
|
||
return textSpan; | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
if (_disposed) | ||
{ | ||
return; | ||
} | ||
|
||
_disposed = true; | ||
_textView.Caret.PositionChanged -= CaretPositionChanged; | ||
_document.Parsed -= OnDocumentParsed; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters