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

Commit 8e91037

Browse files
author
Mikhail Arkhipov
authored
Merge pull request #151 from Microsoft/version
Fix frequent module reloading + add version banner
2 parents 4cf07fb + f4e1c62 commit 8e91037

4 files changed

Lines changed: 66 additions & 15 deletions

File tree

src/LanguageServer/Impl/Implementation/Server.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -377,11 +377,7 @@ private async Task DoInitializeAsync(InitializeParams @params, CancellationToken
377377
}
378378
_displayTextBuilder = DocumentationBuilder.Create(DisplayOptions);
379379

380-
if (string.IsNullOrEmpty(Analyzer.InterpreterFactory?.Configuration?.InterpreterPath)) {
381-
LogMessage(MessageType._General, "Initializing for generic interpreter");
382-
} else {
383-
LogMessage(MessageType._General, $"Initializing for {Analyzer.InterpreterFactory.Configuration.InterpreterPath}");
384-
}
380+
DisplayStartupInfo();
385381

386382
if (@params.rootUri != null) {
387383
_rootDir = @params.rootUri.ToAbsolutePath();
@@ -395,6 +391,14 @@ private async Task DoInitializeAsync(InitializeParams @params, CancellationToken
395391
Analyzer.Interpreter.ModuleNamesChanged += Interpreter_ModuleNamesChanged;
396392
}
397393

394+
private void DisplayStartupInfo() {
395+
LogMessage(MessageType._General, Resources.LanguageServerVersion.FormatInvariant(Assembly.GetExecutingAssembly().GetName().Version));
396+
LogMessage(MessageType._General,
397+
string.IsNullOrEmpty(Analyzer.InterpreterFactory?.Configuration?.InterpreterPath)
398+
? Resources.InitializingForGenericInterpreter
399+
: Resources.InitializingForPythonInterpreter.FormatInvariant(Analyzer.InterpreterFactory.Configuration.InterpreterPath));
400+
}
401+
398402
private void Interpreter_ModuleNamesChanged(object sender, EventArgs e) {
399403
Analyzer.Modules.ReInit();
400404
foreach (var entry in Analyzer.ModulesByFilename) {
@@ -433,7 +437,7 @@ private async Task<PythonAnalyzer> CreateAnalyzer(PythonInitializationOptions.In
433437

434438
var interp = factory.CreateInterpreter();
435439
if (interp == null) {
436-
throw new InvalidOperationException("Failed to create interpreter");
440+
throw new InvalidOperationException(Resources.Error_FailedToCreateInterpreter);
437441
}
438442

439443
LogMessage(MessageType.Info, $"Created {interp.GetType().FullName} instance from {factory.GetType().FullName}");

src/LanguageServer/Impl/PathsWatcher.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,41 +33,40 @@ internal sealed class PathsWatcher : IDisposable {
3333

3434
public PathsWatcher(string[] paths, Action onChanged, ILogger log) {
3535
_log = log;
36-
if (paths?.Length == 0) {
36+
paths = paths != null ? paths.Where(p => Path.IsPathRooted(p)).ToArray() : Array.Empty<string>();
37+
if (paths.Length == 0) {
3738
return;
3839
}
3940

4041
_onChanged = onChanged;
4142

42-
var list = new List<FileSystemWatcher>();
4343
var reduced = ReduceToCommonRoots(paths);
4444
foreach (var p in reduced) {
4545
try {
4646
if (!Directory.Exists(p)) {
4747
continue;
4848
}
49-
} catch(IOException ex) {
49+
} catch (IOException ex) {
5050
_log.TraceMessage($"Unable to access directory {p}, exception {ex.Message}");
5151
continue;
5252
}
5353

5454
try {
5555
var fsw = new System.IO.FileSystemWatcher(p) {
5656
IncludeSubdirectories = true,
57-
EnableRaisingEvents = true
57+
EnableRaisingEvents = true,
58+
NotifyFilter = NotifyFilters.FileName | NotifyFilters.DirectoryName
5859
};
5960

60-
fsw.Changed += OnChanged;
6161
fsw.Created += OnChanged;
6262
fsw.Deleted += OnChanged;
6363

6464
_disposableBag
65-
.Add(() => fsw.Changed -= OnChanged)
6665
.Add(() => fsw.Created -= OnChanged)
6766
.Add(() => fsw.Deleted -= OnChanged)
6867
.Add(() => fsw.EnableRaisingEvents = false)
6968
.Add(fsw);
70-
} catch(ArgumentException ex) {
69+
} catch (ArgumentException ex) {
7170
_log.TraceMessage($"Unable to create file watcher for {p}, exception {ex.Message}");
7271
}
7372
}
@@ -84,9 +83,9 @@ private void OnChanged(object sender, FileSystemEventArgs e) {
8483

8584
private void TimerProc(object o) {
8685
lock (_lock) {
87-
if (!_changedSinceLastTick && _throttleTimer != null) {
86+
if (_changedSinceLastTick) {
8887
ThreadPool.QueueUserWorkItem(_ => _onChanged());
89-
_throttleTimer.Dispose();
88+
_throttleTimer?.Dispose();
9089
_throttleTimer = null;
9190
}
9291
_changedSinceLastTick = false;

src/LanguageServer/Impl/Resources.Designer.cs

Lines changed: 36 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/LanguageServer/Impl/Resources.resx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@
123123
<data name="AnalysisProgress_SingleItemRemaining" xml:space="preserve">
124124
<value>Analyzing workspace, 1 item remaining...</value>
125125
</data>
126+
<data name="Error_FailedToCreateInterpreter" xml:space="preserve">
127+
<value>Failed to create interpreter</value>
128+
</data>
129+
<data name="InitializingForGenericInterpreter" xml:space="preserve">
130+
<value>Initializing for generic interpreter</value>
131+
</data>
132+
<data name="InitializingForPythonInterpreter" xml:space="preserve">
133+
<value>Initializing for {0}</value>
134+
</data>
135+
<data name="LanguageServerVersion" xml:space="preserve">
136+
<value>Microsoft Python Language Server version {0}</value>
137+
</data>
126138
<data name="RenameVariable_CannotRename" xml:space="preserve">
127139
<value>Cannot rename</value>
128140
</data>

0 commit comments

Comments
 (0)