-
Notifications
You must be signed in to change notification settings - Fork 675
Improved tooltip and member doc display and other fixes #4024
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
Merged
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
aec8d9b
Tooltip display improvements
e28931b
Tooltip and completion doc display
2539d87
Test fixes
550356b
Async analyzer creation mode
ec1e672
Merge branch 'master' of https://github.com/Microsoft/PTVS
6d4772d
Move VS Code-specific signature to VSC LS
5b31cc6
Tooltip improvements
c2ce821
Tooltip display
b2f03da
Fix tootip trimming
dcc7333
Merge branch 'master' of https://github.com/Microsoft/PTVS
5025beb
Fix tests
c6e7e9a
Fix test baselines
17d4129
Test fixes
47bc1f6
Test fix
9632f4f
#4031 Concurrency issue accessing analysis dictionary
30d66ef
CR feedback
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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
134 changes: 134 additions & 0 deletions
134
Python/Product/Analysis/LanguageServer/DisplayTextBuilder.cs
This file contains hidden or 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,134 @@ | ||
// Python Tools for Visual Studio | ||
// Copyright(c) Microsoft Corporation | ||
// All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the License); you may not use | ||
// this file except in compliance with the License. You may obtain a copy of the | ||
// License at http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// THIS CODE IS PROVIDED ON AN *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS | ||
// OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY | ||
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, | ||
// MERCHANTABLITY OR NON-INFRINGEMENT. | ||
// | ||
// See the Apache Version 2.0 License for specific language governing | ||
// permissions and limitations under the License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using Microsoft.PythonTools.Analysis.Infrastructure; | ||
using Microsoft.PythonTools.Interpreter; | ||
|
||
namespace Microsoft.PythonTools.Analysis.LanguageServer { | ||
sealed class DisplayTextBuilder { | ||
private readonly RestTextConverter _textConverter = new RestTextConverter(); | ||
|
||
public string MakeHoverText(IEnumerable<AnalysisValue> values, string originalExpression, InformationDisplayOptions displayOptions) { | ||
var result = new StringBuilder(); | ||
var documentations = new HashSet<string>(); | ||
|
||
foreach (var v in values) { | ||
if(result.Length > 0) { | ||
result.AppendLine(); | ||
} | ||
|
||
var doc = GetDocString(v); | ||
doc = displayOptions.trimDocumentationLines ? LimitLines(doc) : doc; | ||
if (string.IsNullOrEmpty(doc)) { | ||
continue; | ||
} | ||
|
||
if (documentations.Add(doc)) { | ||
result.AppendLine(doc); | ||
} | ||
} | ||
|
||
var displayText = result.ToString(); | ||
var multiline = displayText.IndexOf('\n') >= 0; | ||
if (displayOptions.trimDocumentationText && displayText.Length > displayOptions.maxDocumentationTextLength) { | ||
displayText = displayText.Substring(0, | ||
Math.Max(3, displayOptions.maxDocumentationTextLength) - 3) + "..."; | ||
|
||
result.Clear(); | ||
result.Append(displayText); | ||
} | ||
|
||
if (!string.IsNullOrEmpty(originalExpression)) { | ||
if (displayOptions.trimDocumentationText && originalExpression.Length > displayOptions.maxDocumentationTextLength) { | ||
originalExpression = originalExpression.Substring(0, | ||
Math.Max(3, displayOptions.maxDocumentationTextLength) - 3) + "..."; | ||
} | ||
if (multiline) { | ||
result.Insert(0, $"{originalExpression}:{Environment.NewLine}"); | ||
} else if (result.Length > 0) { | ||
result.Insert(0, $"{originalExpression}: "); | ||
} else { | ||
result.Append($"{originalExpression}: <unknown type>"); | ||
} | ||
} | ||
|
||
return _textConverter.ToMarkdown(result.ToString()); | ||
} | ||
|
||
public string MakeModuleHoverText(ModuleReference modRef) { | ||
// Return module information | ||
var contents = "{0} : module".FormatUI(modRef.Name); | ||
if (!string.IsNullOrEmpty(modRef.Module?.Documentation)) { | ||
contents += $"{Environment.NewLine}{Environment.NewLine}{modRef.Module.Documentation}"; | ||
} | ||
return contents; | ||
} | ||
|
||
private static string GetDocString(AnalysisValue v) { | ||
var doc = !string.IsNullOrEmpty(v.Documentation) ? v.Documentation : string.Empty; | ||
var desc = !string.IsNullOrEmpty(v.Description) ? v.Description : string.Empty; | ||
if (v.MemberType == PythonMemberType.Instance || v.MemberType == PythonMemberType.Constant) { | ||
return !string.IsNullOrEmpty(desc) ? desc : doc; | ||
} | ||
return doc.Length > desc.Length ? doc : desc; | ||
} | ||
|
||
private static string LimitLines( | ||
string str, | ||
int maxLines = 30, | ||
int charsPerLine = 200, | ||
bool ellipsisAtEnd = true, | ||
bool stopAtFirstBlankLine = false | ||
) { | ||
if (string.IsNullOrEmpty(str)) { | ||
return str; | ||
} | ||
|
||
var lineCount = 0; | ||
var prettyPrinted = new StringBuilder(); | ||
var wasEmpty = true; | ||
|
||
using (var reader = new StringReader(str)) { | ||
for (var line = reader.ReadLine(); line != null && lineCount < maxLines; line = reader.ReadLine()) { | ||
if (string.IsNullOrWhiteSpace(line)) { | ||
if (wasEmpty) { | ||
continue; | ||
} | ||
wasEmpty = true; | ||
if (stopAtFirstBlankLine) { | ||
lineCount = maxLines; | ||
break; | ||
} | ||
lineCount += 1; | ||
prettyPrinted.AppendLine(); | ||
} else { | ||
wasEmpty = false; | ||
lineCount += (line.Length / charsPerLine) + 1; | ||
prettyPrinted.AppendLine(line); | ||
} | ||
} | ||
} | ||
if (ellipsisAtEnd && lineCount >= maxLines) { | ||
prettyPrinted.AppendLine("..."); | ||
} | ||
return prettyPrinted.ToString().Trim(); | ||
} | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need
FormatUI
here as well?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It just does
CurrentCulture
... I am actually not sure that current culture is right. Here we should have invariant I believe. I.e. if we have programming language docs then numbers must remain5.0
and should not be displayed as5,0
in a culture-specific manner.