Skip to content
This repository was archived by the owner on Apr 14, 2022. It is now read-only.

Fix couple of bugs #566

Merged
merged 3 commits into from
Jan 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion src/Analysis/Engine/Impl/Definitions/Diagnostic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class Diagnostic {
}

public enum DiagnosticSeverity : int {
Unspecified = 0,
Suppressed = 0,
Error = 1,
Warning = 2,
Information = 3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private PathResolverSnapshot(PythonLanguageVersion pythonLanguageVersion, string

public IEnumerable<string> GetAllModuleNames() => GetModuleNames(_roots.Prepend(_nonRooted).Append(_builtins));
public IEnumerable<string> GetInterpreterModuleNames() => GetModuleNames(_roots.Skip(_userRootsCount).Append(_builtins));
public IEnumerable<string> GetRootPaths() => _roots.Select(r => r.Name);

private static IEnumerable<string> GetModuleNames(IEnumerable<Node> roots) => roots
.SelectMany(r => r.TraverseBreadthFirst(n => n.IsModule ? Enumerable.Empty<Node>() : n.Children))
Expand Down
13 changes: 10 additions & 3 deletions src/Analysis/Engine/Impl/PythonAnalyzer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public partial class PythonAnalyzer : IPythonAnalyzer, IDisposable {
public static async Task<PythonAnalyzer> CreateAsync(IPythonInterpreterFactory factory, CancellationToken token = default) {
var analyzer = new PythonAnalyzer(factory);
try {
await analyzer.ReloadModulesAsync(token).ConfigureAwait(false);
await analyzer.ReloadModulesAsync(false, token).ConfigureAwait(false);
} catch(Exception) {
analyzer.Dispose();
throw;
Expand Down Expand Up @@ -140,7 +140,9 @@ private void ReloadModulePaths(in IEnumerable<string> rootPaths) {
/// This method should be called on the analysis thread and is usually invoked
/// when the interpreter signals that it's modules have changed.
/// </summary>
public async Task ReloadModulesAsync(CancellationToken token = default) {
public Task ReloadModulesAsync(CancellationToken token = default) => ReloadModulesAsync(true, token);

private async Task ReloadModulesAsync(bool reScanRoots, CancellationToken token) {
if (!_reloadLock.Wait(0)) {
// If we don't lock immediately, wait for the current reload to
// complete and then return.
Expand All @@ -156,6 +158,11 @@ public async Task ReloadModulesAsync(CancellationToken token = default) {
InterpreterFactory.NotifyImportNamesChanged();
// Now initialize the interpreter
Interpreter.Initialize(this);
// Rescan roots for modules if requested
if (reScanRoots) {
ReloadModulePaths(CurrentPathResolver.GetRootPaths());
}

// Reload importable modules
Modules.Reload();
// Load known types from the selected interpreter
Expand Down Expand Up @@ -760,7 +767,7 @@ public void AnalyzeQueuedEntries(CancellationToken cancel) {

if (_builtinModule == null) {
Debug.Fail("Used analyzer without reloading modules");
ReloadModulesAsync(cancel).WaitAndUnwrapExceptions();
ReloadModulesAsync(true, cancel).WaitAndUnwrapExceptions();
}

var ddg = new DDG();
Expand Down
6 changes: 3 additions & 3 deletions src/Analysis/Engine/Test/LanguageServerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ public async Task ParseIndentationDiagnostics() {
DiagnosticSeverity.Error,
DiagnosticSeverity.Warning,
DiagnosticSeverity.Information,
DiagnosticSeverity.Unspecified
DiagnosticSeverity.Suppressed
}) {
// For now, these options have to be configured directly
s.ParseQueue.InconsistentIndentation = tc;
Expand All @@ -317,7 +317,7 @@ await s.DidChangeTextDocument(new DidChangeTextDocumentParams {
await s.WaitForCompleteAnalysisAsync(CancellationToken.None);

var messages = GetDiagnostics(diags, mod).ToArray();
if (tc == DiagnosticSeverity.Unspecified) {
if (tc == DiagnosticSeverity.Suppressed) {
messages.Should().BeEmpty();
} else {
messages.Should().OnlyContain($"{tc};inconsistent whitespace;Python;2;0;1");
Expand Down Expand Up @@ -359,7 +359,7 @@ public async Task DiagnosticsSettingChange() {
await s.SendDidChangeConfiguration(newSettings);

await s.WaitForCompleteAnalysisAsync(CancellationToken.None);
GetDiagnostics(diags, u).Where(st => !st.StartsWith($"{DiagnosticSeverity.Unspecified}")).Should().BeEmpty();
GetDiagnostics(diags, u).Where(st => !st.StartsWith($"{DiagnosticSeverity.Suppressed}")).Should().BeEmpty();
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/LanguageServer/Core/Impl/Definitions/ServerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void SetErrorSeverityOptions(string[] errors, string[] warnings, string[]
_map[x] = DiagnosticSeverity.Error;
}
foreach (var x in disabled) {
_map[x] = DiagnosticSeverity.Unspecified;
_map[x] = DiagnosticSeverity.Suppressed;
}

this.errors = errors;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,18 @@ public void ProcessTaskComment(object sender, CommentEventArgs e) {

internal static DiagnosticSeverity GetSeverity(Severity severity) {
switch (severity) {
case Severity.Ignore: return DiagnosticSeverity.Unspecified;
case Severity.Ignore: return DiagnosticSeverity.Suppressed;
case Severity.Information: return DiagnosticSeverity.Information;
case Severity.Warning: return DiagnosticSeverity.Warning;
case Severity.Error: return DiagnosticSeverity.Error;
case Severity.FatalError: return DiagnosticSeverity.Error;
default: return DiagnosticSeverity.Unspecified;
default: return DiagnosticSeverity.Suppressed;
}
}

internal static Severity GetSeverity(DiagnosticSeverity severity) {
switch (severity) {
case DiagnosticSeverity.Unspecified: return Severity.Ignore;
case DiagnosticSeverity.Suppressed: return Severity.Ignore;
case DiagnosticSeverity.Information: return Severity.Information;
case DiagnosticSeverity.Warning: return Severity.Warning;
case DiagnosticSeverity.Error: return Severity.Error;
Expand Down
2 changes: 1 addition & 1 deletion src/LanguageServer/Impl/LanguageServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ private void PublishPendingDiagnostics() {
foreach (var kvp in list) {
var parameters = new PublishDiagnosticsParams {
uri = kvp.Key,
diagnostics = kvp.Value.Where(d => d.severity != DiagnosticSeverity.Unspecified).ToArray()
diagnostics = kvp.Value.Where(d => d.severity != DiagnosticSeverity.Suppressed).ToArray()
};
_rpc.NotifyWithParameterObjectAsync("textDocument/publishDiagnostics", parameters).DoNotWait();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ public sealed class InitializeRequest : Request<InitializeResponse> {
public bool analyzeAllFiles;
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool traceLogging;
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Ignore)]
public bool liveLinting;
}

public sealed class InterpreterInfo {
Expand Down Expand Up @@ -824,6 +822,8 @@ public sealed class SetAnalysisOptionsRequest : Request<Response> {

public sealed class AnalysisOptions {
public Severity indentationInconsistencySeverity;
public bool enableUnresolvedImportWarning;
public bool enableUseBeforeDefWarning;
public Dictionary<string, DiagnosticSeverity> commentTokens;
public Dictionary<string, int> analysisLimits;
public LS.MessageType? traceLevel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,7 @@ await _server.Initialize(new LS.InitializeParams {
},
capabilities = new LS.ClientCapabilities {
python = new LS.PythonClientCapabilities {
manualFileLoad = !request.analyzeAllFiles,
liveLinting = request.liveLinting
manualFileLoad = !request.analyzeAllFiles
},
textDocument = new LS.TextDocumentClientCapabilities {
completion = new LS.TextDocumentClientCapabilities.CompletionCapabilities {
Expand Down Expand Up @@ -1559,9 +1558,35 @@ private Response SetAnalysisOptions(AP.SetAnalysisOptionsRequest request) {
_server.ParseQueue.TaskCommentMap = Options.commentTokens;
_server.Analyzer.SetTypeStubPaths(Options.typeStubPaths ?? Enumerable.Empty<string>());

SetErrorMessagesSeverity();

return new Response();
}

private void SetErrorMessagesSeverity() {
var serverSettings = new LS.ServerSettings();
var warnings = new List<string>();
var disabled = new List<string>();

if (Options.enableUnresolvedImportWarning) {
warnings.Add(ErrorMessages.UnresolvedImportCode);
} else {
disabled.Add(ErrorMessages.UnresolvedImportCode);
}

if (Options.enableUseBeforeDefWarning) {
warnings.Add(ErrorMessages.UseBeforeDefCode);
} else {
disabled.Add(ErrorMessages.UseBeforeDefCode);
}

serverSettings.analysis.SetErrorSeverityOptions(Array.Empty<string>(), warnings.ToArray(), Array.Empty<string>(), disabled.ToArray());

_server.DidChangeConfiguration(new LS.DidChangeConfigurationParams {
settings = serverSettings
}, CancellationToken.None).DoNotWait();
}


public AP.AnalysisOptions Options { get; set; }

Expand All @@ -1570,7 +1595,7 @@ private void AnalysisQueue_Complete(object sender, EventArgs e) {
}

private void OnModulesChanged(object sender, EventArgs args) {
_server.DidChangeConfiguration(new LS.DidChangeConfigurationParams(), CancellationToken.None).DoNotWait();
_server.ReloadModulesAsync(CancellationToken.None).DoNotWait();
}

private void OnFileChanged(AP.FileChangedEvent e) {
Expand Down Expand Up @@ -1653,7 +1678,7 @@ private void OnPublishDiagnostics(object sender, LS.PublishDiagnosticsEventArgs
new AP.DiagnosticsEvent {
documentUri = e.uri,
version = e._version ?? -1,
diagnostics = e.diagnostics?.ToArray()
diagnostics = e.diagnostics?.Where(d => d.severity != DiagnosticSeverity.Suppressed).ToArray()
}
).DoNotWait();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<file src="Microsoft.PythonTools.Analyzer.exe" target="lib/vs16" />
<file src="Microsoft.PythonTools.Analyzer.exe.config" target="lib/vs16" />
<file src="Microsoft.PythonTools.Analyzer.pdb" target="lib/vs16" />
<file src="Typeshed/**" target="lib/vs16/Typeshed" />
<file src="Typeshed/**" target="lib/vs16" />
<file src="PythonScraper.py" target="lib/vs16" />
<file src="BuiltinScraper.py" target="lib/vs16" />
<file src="ExtensionScraper.py" target="lib/vs16" />
Expand Down