PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/Python/Product/PythonTools/PythonTools/Intellisense/PythonSuggestedImportAction.cs

https://gitlab.com/SplatoonModdingHub/PTVS
C# | 181 lines | 132 code | 28 blank | 21 comment | 18 complexity | ec24fff2a187b08bf0450aa21b9ed8f9 MD5 | raw file
  1. // Python Tools for Visual Studio
  2. // Copyright(c) Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the License); you may not use
  6. // this file except in compliance with the License. You may obtain a copy of the
  7. // License at http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS
  10. // OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY
  11. // IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  12. // MERCHANTABLITY OR NON-INFRINGEMENT.
  13. //
  14. // See the Apache Version 2.0 License for specific language governing
  15. // permissions and limitations under the License.
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Diagnostics;
  19. using System.Linq;
  20. using System.Threading;
  21. using System.Threading.Tasks;
  22. using System.Windows.Media;
  23. using Microsoft.PythonTools.Analysis;
  24. using Microsoft.VisualStudio.Imaging.Interop;
  25. using Microsoft.VisualStudio.Language.Intellisense;
  26. using Microsoft.VisualStudio.Text;
  27. namespace Microsoft.PythonTools.Intellisense {
  28. class PythonSuggestedImportAction : ISuggestedAction, IComparable<PythonSuggestedImportAction> {
  29. private readonly PythonSuggestedActionsSource _source;
  30. private readonly string _name;
  31. private readonly string _fromModule;
  32. private readonly ITextBuffer _buffer;
  33. public PythonSuggestedImportAction(PythonSuggestedActionsSource source, ITextBuffer buffer, ExportedMemberInfo import) {
  34. _source = source;
  35. _fromModule = import.FromName;
  36. _name = import.ImportName;
  37. _buffer = buffer;
  38. }
  39. public IEnumerable<SuggestedActionSet> ActionSets {
  40. get {
  41. return Enumerable.Empty<SuggestedActionSet>();
  42. }
  43. }
  44. public Task<IEnumerable<SuggestedActionSet>> GetActionSetsAsync(CancellationToken cancellationToken) {
  45. return Task.FromResult(ActionSets);
  46. }
  47. public bool HasActionSets {
  48. get { return false; }
  49. }
  50. public string DisplayText {
  51. get {
  52. return MakeImportCode(_fromModule, _name)
  53. .Replace("_", "__");
  54. }
  55. }
  56. private static string MakeImportCode(string fromModule, string name) {
  57. if (string.IsNullOrEmpty(fromModule)) {
  58. return string.Format("import {0}", name);
  59. } else {
  60. return string.Format("from {0} import {1}", fromModule, name);
  61. }
  62. }
  63. public string IconAutomationText {
  64. get {
  65. return null;
  66. }
  67. }
  68. public ImageMoniker IconMoniker {
  69. get {
  70. return default(ImageMoniker);
  71. }
  72. }
  73. public ImageSource IconSource {
  74. get {
  75. // TODO: Convert from IconMoniker
  76. return null;
  77. }
  78. }
  79. public string InputGestureText {
  80. get {
  81. return null;
  82. }
  83. }
  84. public void Dispose() { }
  85. public object GetPreview(CancellationToken cancellationToken) {
  86. return null;
  87. }
  88. public Task<object> GetPreviewAsync(CancellationToken cancellationToken) {
  89. return Task.FromResult<object>(null);
  90. }
  91. public bool HasPreview {
  92. get { return false; }
  93. }
  94. public async void Invoke(CancellationToken cancellationToken) {
  95. Debug.Assert(!string.IsNullOrEmpty(_name));
  96. await VsProjectAnalyzer.AddImportAsync(
  97. _source._view.GetAnalysisEntry(_buffer, _source._provider),
  98. _fromModule,
  99. _name,
  100. _source._view,
  101. _buffer
  102. );
  103. }
  104. public bool TryGetTelemetryId(out Guid telemetryId) {
  105. telemetryId = Guid.Empty;
  106. return false;
  107. }
  108. public override bool Equals(object obj) {
  109. var other = obj as PythonSuggestedImportAction;
  110. if (other == null) {
  111. return false;
  112. }
  113. return DisplayText.Equals(other.DisplayText);
  114. }
  115. public override int GetHashCode() {
  116. return DisplayText.GetHashCode();
  117. }
  118. public int CompareTo(PythonSuggestedImportAction other) {
  119. if (other == null) {
  120. return -1;
  121. }
  122. // Sort from ... import before import ...
  123. if (!string.IsNullOrEmpty(_fromModule)) {
  124. if (string.IsNullOrEmpty(other._fromModule)) {
  125. return -1;
  126. }
  127. } else if (!string.IsNullOrEmpty(other._fromModule)) {
  128. return 1;
  129. }
  130. var key1 = _fromModule ?? _name ?? "";
  131. var key2 = other._fromModule ?? other._name ?? "";
  132. // Name with fewer dots sorts first
  133. var dotCount1 = key1.Count(c => c == '.');
  134. var dotCount2 = key2.Count(c => c == '.');
  135. int r = dotCount1.CompareTo(dotCount2);
  136. if (r != 0) {
  137. return r;
  138. }
  139. // Shorter name sorts first
  140. r = key1.Length.CompareTo(key2.Length);
  141. if (r != 0) {
  142. return r;
  143. }
  144. // Keys sort alphabetically
  145. r = key1.CompareTo(key2);
  146. if (r != 0) {
  147. return r;
  148. }
  149. // Sort by display text
  150. return (DisplayText ?? "").CompareTo(other.DisplayText ?? "");
  151. }
  152. }
  153. }