Skip to content

Commit

Permalink
Merge pull request #137 from tareqimbasher/fixes
Browse files Browse the repository at this point in the history
Fixes and enhancements
  • Loading branch information
tareqimbasher authored Dec 10, 2023
2 parents 25827f3 + 4f8f200 commit 57c13c7
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,10 @@ export class TextChangeUtil {
}

private static getNamespacesFromUsings(textChange: LinePositionSpanTextChange): string[] {
return textChange.newText?.split("\n")
.filter(l => !!l && l.trim())
return textChange.newText
?.split("\n")
.map(l => l?.trim()) // to remove empty parts
.filter(l => !!l)
.map(l => Util.trimWord(l, "using "))
.map(l => Util.trimEnd(l, ";"))
.map(l => l.trim())
Expand Down
2 changes: 1 addition & 1 deletion src/Apps/NetPad.Apps.App/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void ConfigureServices(IServiceCollection services)
services.AddTransient<IAssemblyLoader, UnloadableAssemblyLoader>();
services.AddTransient<ILogoService, LogoService>();
services.AddSingleton<IAppStatusMessagePublisher, AppStatusMessagePublisher>();
services.AddSingleton<IKeyValueDataStore, FileSystemKeyValueDataStore>();
services.AddSingleton<ITrivialDataStore, FileSystemTrivialDataStore>();

// Scripts
services.AddTransient<IScriptRepository, FileSystemScriptRepository>();
Expand Down
25 changes: 20 additions & 5 deletions src/Core/NetPad.Application/Sessions/Session.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Extensions.Logging;
using NetPad.Data;
using NetPad.Events;
using NetPad.Exceptions;
using NetPad.Scripts;
Expand All @@ -7,18 +8,23 @@ namespace NetPad.Sessions;

public class Session : ISession
{
private const string CurrentActiveSaveKey = "session.active";

private readonly IScriptEnvironmentFactory _scriptEnvironmentFactory;
private readonly ITrivialDataStore _trivialDataStore;
private readonly IEventBus _eventBus;
private readonly ILogger<Session> _logger;
private readonly List<ScriptEnvironment> _environments;
private Guid? _lastActiveScriptId;

public Session(
IScriptEnvironmentFactory scriptEnvironmentFactory,
ITrivialDataStore trivialDataStore,
IEventBus eventBus,
ILogger<Session> logger)
{
_scriptEnvironmentFactory = scriptEnvironmentFactory;
_trivialDataStore = trivialDataStore;
_eventBus = eventBus;
_logger = logger;
_environments = new List<ScriptEnvironment>();
Expand Down Expand Up @@ -53,17 +59,21 @@ public async Task OpenAsync(Script script, bool activate = true)

public async Task OpenAsync(IEnumerable<Script> scripts)
{
Script? last = null;
scripts = scripts.ToArray();

foreach (var script in scripts)
{
await OpenAsync(script, activate: false);
last = script;
}

if (last != null)
if (Guid.TryParse(_trivialDataStore.Get<string>(CurrentActiveSaveKey), out var lastSavedScriptId) &&
scripts.Any(s => s.Id == lastSavedScriptId))
{
await ActivateAsync(lastSavedScriptId);
}
else
{
await ActivateAsync(last.Id);
await ActivateAsync(scripts.LastOrDefault()?.Id);
}
}

Expand Down Expand Up @@ -126,7 +136,9 @@ public Task ActivateAsync(Guid? scriptId)
}

if (scriptId == null)
{
newActive = null;
}
else
{
var environment = Get(scriptId.Value);
Expand All @@ -136,6 +148,8 @@ public Task ActivateAsync(Guid? scriptId)
Active = newActive;
_eventBus.PublishAsync(new ActiveEnvironmentChangedEvent(newActive?.Script.Id));

_trivialDataStore.Set(CurrentActiveSaveKey, Active?.Script.Id);

return Task.CompletedTask;
}

Expand All @@ -147,5 +161,6 @@ public Task ActivateLastActiveScriptAsync()
return Task.CompletedTask;
}

private bool CanActivateLastActiveScript() => _lastActiveScriptId != null && _environments.Any(e => e.Script.Id == _lastActiveScriptId);
private bool CanActivateLastActiveScript() =>
_lastActiveScriptId != null && _environments.Any(e => e.Script.Id == _lastActiveScriptId);
}
11 changes: 0 additions & 11 deletions src/Core/NetPad.Domain/Data/IKeyValueDataStore.cs

This file was deleted.

12 changes: 12 additions & 0 deletions src/Core/NetPad.Domain/Data/ITrivialDataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace NetPad.Data;

/// <summary>
/// Represents a key-value data store that stores trivial data. Trivial data is data
/// that is not essential to the functionality of the application or the user experience.
/// </summary>
public interface ITrivialDataStore
{
TValue? Get<TValue>(string key) where TValue : class;
void Set<TValue>(string key, TValue value);
bool Contains(string key);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ namespace NetPad.Electron.UiInterop;
public class ElectronWindowService : IUiWindowService
{
private readonly WindowManager _windowManager;
private readonly IKeyValueDataStore _keyValueDataStore;
private readonly ITrivialDataStore _trivialDataStore;
private readonly Settings _settings;
private readonly ILogger<ElectronWindowService> _logger;

public ElectronWindowService(WindowManager windowManager, IKeyValueDataStore keyValueDataStore, Settings settings, ILogger<ElectronWindowService> logger)
public ElectronWindowService(WindowManager windowManager, ITrivialDataStore trivialDataStore, Settings settings, ILogger<ElectronWindowService> logger)
{
_windowManager = windowManager;
_keyValueDataStore = keyValueDataStore;
_trivialDataStore = trivialDataStore;
_settings = settings;
_logger = logger;
}
Expand All @@ -44,7 +44,7 @@ private async Task RestoreMainWindowPositionAsync(BrowserWindow window)
{
try
{
var savedBounds = _keyValueDataStore.Get<Rectangle>("main-window.bounds");
var savedBounds = _trivialDataStore.Get<Rectangle>("main-window.bounds");
if (savedBounds != null)
{
window.SetBounds(savedBounds);
Expand All @@ -67,7 +67,7 @@ private async Task RestoreMainWindowPositionAsync(BrowserWindow window)

window.OnClose += async () =>
{
_keyValueDataStore.Set("main-window.bounds", await window.GetBoundsAsync());
_trivialDataStore.Set("main-window.bounds", await window.GetBoundsAsync());
};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace NetPad;

public class FileSystemKeyValueDataStore : IKeyValueDataStore
public class FileSystemTrivialDataStore : ITrivialDataStore
{
private static readonly FilePath _storeFilePath = AppDataProvider.AppDataDirectoryPath.CombineFilePath("key-values.txt");
private static readonly object _fileLock = new();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -277,10 +277,7 @@ private void InitializeEventHandlers()
Subscribe<ScriptNamespacesUpdatedEvent>(async ev =>
{
if (ev.Script.Id != _environment.Script.Id) return;

var script = _environment.Script;
var parsingResult = _codeParser.Parse(script.Code, script.Config.Kind, script.Config.Namespaces);
await UpdateOmniSharpCodeBufferWithBootstrapperProgramAsync(parsingResult);
await UpdateOmniSharpCodeBufferAsync();
});

Subscribe<ScriptReferencesUpdatedEvent>(async ev =>
Expand Down Expand Up @@ -385,8 +382,6 @@ private async Task UpdateOmniSharpCodeBufferWithDataConnectionAsync(DataConnecti
// Needed to trigger diagnostics and semantic highlighting for script file
await Task.Delay(1000);
await UpdateOmniSharpCodeBufferAsync();

await _eventBus.PublishAsync(new OmniSharpAsyncBufferUpdateCompletedEvent(_environment.Script.Id));
}

private async Task UpdateOmniSharpCodeBufferWithDataConnectionProgramAsync(Task<DataConnectionSourceCode>? sourceCodeTask)
Expand Down Expand Up @@ -438,5 +433,7 @@ await OmniSharpServer.SendAsync(new UpdateBufferRequest
{
semaphore.Release();
}

await _eventBus.PublishAsync(new OmniSharpAsyncBufferUpdateCompletedEvent(_environment.Script.Id));
}
}
2 changes: 2 additions & 0 deletions src/Tests/NetPad.Tests/Helpers/SessionTestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using NetPad.Events;
using NetPad.Scripts;
using NetPad.Sessions;
using NetPad.Tests.Services;

namespace NetPad.Tests.Helpers;

Expand All @@ -13,6 +14,7 @@ public static Session CreateSession(IServiceProvider serviceProvider)
{
return new Session(
new DefaultScriptEnvironmentFactory(serviceProvider),
new NullTrivialDataStore(),
serviceProvider.GetRequiredService<IEventBus>(),
serviceProvider.GetRequiredService<ILogger<Session>>());
}
Expand Down
20 changes: 20 additions & 0 deletions src/Tests/NetPad.Tests/Services/NullTrivialDataStore.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using NetPad.Data;

namespace NetPad.Tests.Services;

public class NullTrivialDataStore : ITrivialDataStore
{
public TValue? Get<TValue>(string key) where TValue : class
{
return null;
}

public void Set<TValue>(string key, TValue value)
{
}

public bool Contains(string key)
{
return false;
}
}

0 comments on commit 57c13c7

Please sign in to comment.