Skip to content
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
11 changes: 6 additions & 5 deletions AGENTS.md

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src/TuiCode.Abstractions/CommandIds.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static class CommandIds
public const string GoToMatchingBracket = "workbench.action.goToMatchingBracket";

public const string GoToDefinition = "workbench.action.goToDefinition";
public const string GoToImplementation = "workbench.action.goToImplementation";
public const string QuickInfo = "workbench.action.quickInfo";
public const string QuickInfoClose = "quickInfo.action.close";
public const string QuickInfoGoToDefinition = "quickInfo.action.goToDefinition";
Expand Down
8 changes: 4 additions & 4 deletions src/TuiCode.Abstractions/CommandMnemonics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@ namespace TuiCode.Abstractions;
/// A mnemonic is the command label's initials (<c>fg</c> for "Find globally", <c>nt</c> for
/// "Next tab"); a label that doesn't give usable initials is the thing to change.
///
/// Invariant: no complete mnemonic is a prefix of another. That lets the dialog
/// auto-execute the instant the typed sequence uniquely identifies a command, with no
/// terminating Enter. Mnemonics therefore come in families under a shared first key
/// The dialog runs a mnemonic the instant it's typed, unless a longer one starts with it
/// (<c>q</c> under <c>qi</c>): that one waits for Enter. Mnemonics come in families under a shared first key
/// (<c>c</c>lose, <c>o</c>pen, <c>f</c>ocus, <c>t</c>oggle, …) that have room to
/// grow: <c>cf</c> close file leaves <c>co</c>/<c>ca</c>/<c>cs</c> free for future
/// close-other/all/sidebar commands. <c>q</c> (quit) and <c>?</c> (help) are the only
Expand Down Expand Up @@ -109,7 +108,8 @@ private static IReadOnlyDictionary<string, string> Build()
[CommandIds.GoToType] = "gt",
[CommandIds.GoToMatchingBracket] = "gm",
[CommandIds.GoToDefinition] = "gd",
[CommandIds.QuickInfo] = "gi",
[CommandIds.GoToImplementation] = "gi",
[CommandIds.QuickInfo] = "qi",
[CommandIds.FindUsages] = "gu",
[CommandIds.AiAssistance] = "ai",
[CommandIds.FocusAssistant] = "fi",
Expand Down
5 changes: 3 additions & 2 deletions src/TuiCode.Workbench/Languages/KnownLanguageServers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace TuiCode.Workbench.Languages;

/// <summary>A language server TuiCode can offer by name, with what to run and how to install it.</summary>
public sealed record KnownLanguageServer(string Name, string Command, IReadOnlyList<string> Arguments, string Install)
/// <param name="Title">The name in a sentence, where it isn't <paramref name="Name"/>.</param>
public sealed record KnownLanguageServer(string Name, string Command, IReadOnlyList<string> Arguments, string Install, string? Title = null)
{
public LanguageServerSetting Setting { get; } = new(Command, Arguments);
}
Expand All @@ -24,7 +25,7 @@ public static class KnownLanguageServers
private static readonly KnownLanguageServer TypeScript = new(
"typescript-language-server", "typescript-language-server", ["--stdio"], "npm install -g typescript-language-server typescript");

private static readonly KnownLanguageServer Pyright = new("pyright", "pyright-langserver", ["--stdio"], "npm install -g pyright");
private static readonly KnownLanguageServer Pyright = new("pyright", "pyright-langserver", ["--stdio"], "npm install -g pyright", "Pyright");

private static readonly KnownLanguageServer Pylsp = new("pylsp", "pylsp", [], "pip install python-lsp-server");

Expand Down
24 changes: 23 additions & 1 deletion src/TuiCode.Workbench/Languages/LanguageServer.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.Json;
using System.Text.Json.Nodes;

namespace TuiCode.Workbench.Languages;
Expand All @@ -8,6 +9,9 @@ public sealed record LanguageServerSpec(string LanguageId, string Name, string C
public static readonly LanguageServerSpec CSharp = new(
"csharp", "C#", KnownLanguageServers.CSharpLs.Command, KnownLanguageServers.CSharpLs.Arguments, KnownLanguageServers.CSharpLs.Install);

/// <summary>The server's name in a sentence: <c>Pyright</c> for <c>pyright-langserver</c>.</summary>
public string Title => KnownLanguageServers.WithCommand(LanguageId, Command) is { } known ? known.Title ?? known.Name : Command;

public string Loading => $"{Command} is loading the {(LanguageId == "csharp" ? "solution" : "folder")}…";

public string NotInstalled => Install is null
Expand Down Expand Up @@ -44,6 +48,7 @@ public LanguageServer(LanguageServerSpec spec, string root, ILanguageServerLaunc
Root = root;
_launcher = launcher;
_post = post;
Title = spec.Title;
}

public LanguageServerSpec Spec { get; }
Expand All @@ -59,6 +64,12 @@ public LanguageServer(LanguageServerSpec spec, string root, ILanguageServerLaunc
/// <summary>How long stopping waits for the server to answer shutdown, and then to exit, before ending it.</summary>
public TimeSpan StopTimeout { get; init; } = TimeSpan.FromSeconds(1);

/// <summary>Whether the server answers <c>textDocument/implementation</c>, as its initialize result says.</summary>
public bool FindsImplementations { get; private set; }

/// <summary>What TuiCode knows the server as, or else what it calls itself.</summary>
public string Title { get; private set; }

public event EventHandler? StateChanged;

/// <summary>The server's problems for an open file, replacing any it reported before. Raised through <c>post</c>.</summary>
Expand Down Expand Up @@ -91,7 +102,11 @@ private async Task InitializeAsync(JsonRpcConnection connection)
{
try
{
await connection.RequestAsync("initialize", InitializeParams()).ConfigureAwait(false);
var initialized = await connection.RequestAsync("initialize", InitializeParams()).ConfigureAwait(false);
FindsImplementations = initialized?["capabilities"]?["implementationProvider"]?.GetValueKind() is JsonValueKind.Object or JsonValueKind.True;
if (KnownLanguageServers.WithCommand(Spec.LanguageId, Spec.Command) is null
&& initialized?["serverInfo"]?["name"]?.GetValue<string>() is { Length: > 0 } name)
Title = name;
}
catch (Exception e) when (e is JsonRpcException or IOException)
{
Expand Down Expand Up @@ -142,6 +157,7 @@ private JsonObject InitializeParams()
["publishDiagnostics"] = new JsonObject(),
["synchronization"] = new JsonObject { ["didSave"] = true },
["definition"] = new JsonObject { ["linkSupport"] = true },
["implementation"] = new JsonObject { ["linkSupport"] = true },
["documentSymbol"] = new JsonObject { ["hierarchicalDocumentSymbolSupport"] = true },
["hover"] = new JsonObject { ["contentFormat"] = new JsonArray("markdown", "plaintext") },
},
Expand Down Expand Up @@ -289,6 +305,12 @@ private string UriOf(string path)
public async Task<IReadOnlyList<SourceLocation>> DefinitionAsync(string path, int line, int character) =>
LspLocations.Parse(await RequestAsync("textDocument/definition", Position(path, line, character)).ConfigureAwait(false));

/// <summary>What implements the interface, abstract member or type at <paramref name="line"/> and UTF-16 <paramref name="character"/>; nothing from a server that can't say.</summary>
public async Task<IReadOnlyList<SourceLocation>> ImplementationAsync(string path, int line, int character) =>
FindsImplementations
? LspLocations.Parse(await RequestAsync("textDocument/implementation", Position(path, line, character)).ConfigureAwait(false))
: [];

/// <summary>What the server says the symbol at <paramref name="line"/> and UTF-16 <paramref name="character"/> is.</summary>
public async Task<IReadOnlyList<HoverPart>> HoverAsync(string path, int line, int character) =>
LspHover.Parse(await RequestAsync("textDocument/hover", Position(path, line, character)).ConfigureAwait(false));
Expand Down
4 changes: 2 additions & 2 deletions src/TuiCode.Workbench/Menus/CommandMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ .. Enumerable.Range(1, 9).Select(CommandIds.FocusEditorByIndex),
[CommandIds.JoinGroups] = 'j', [CommandIds.ToggleFileHistory] = 'y', [CommandIds.RevealInExplorer] = 'x',
[CommandIds.FocusAssistant] = 'a', [CommandIds.AiAssistance] = 'h', [CommandIds.ToggleToolCalls] = 'c',

[CommandIds.GoToLine] = 'g', [CommandIds.GoToSymbol] = 's', [CommandIds.GoToType] = 'y', [CommandIds.GoToMatchingBracket] = 'm', [CommandIds.GoToDefinition] = 'd', [CommandIds.QuickInfo] = 'i',
[CommandIds.GoToLine] = 'g', [CommandIds.GoToSymbol] = 's', [CommandIds.GoToType] = 'y', [CommandIds.GoToMatchingBracket] = 'm', [CommandIds.GoToDefinition] = 'd', [CommandIds.GoToImplementation] = 'i', [CommandIds.QuickInfo] = 'q',
[CommandIds.FindUsages] = 'f', [CommandIds.FollowLink] = 'l', [CommandIds.NavigateBack] = 'p',
[CommandIds.NavigateForward] = 'n', [CommandIds.NextEditor] = 't', [CommandIds.PreviousEditor] = 'r',

Expand Down Expand Up @@ -128,7 +128,7 @@ public static readonly (string Title, string[] Ids)[] Layout =
]),
("_Go",
[
CommandIds.GoToLine, CommandIds.GoToSymbol, CommandIds.GoToType, CommandIds.GoToMatchingBracket, CommandIds.GoToDefinition, CommandIds.QuickInfo, CommandIds.FindUsages, CommandIds.FollowLink,
CommandIds.GoToLine, CommandIds.GoToSymbol, CommandIds.GoToType, CommandIds.GoToMatchingBracket, CommandIds.GoToDefinition, CommandIds.GoToImplementation, CommandIds.QuickInfo, CommandIds.FindUsages, CommandIds.FollowLink,
Separator,
CommandIds.NavigateBack, CommandIds.NavigateForward,
Separator,
Expand Down
24 changes: 14 additions & 10 deletions src/TuiCode.Workbench/Mnemonics/MnemonicResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ public sealed record MnemonicEntry(string CommandId, string Mnemonic, string Lab
///
/// As the user types, the dialog feeds the accumulated prefix here. <see cref="Matching"/>
/// drives the filtered hint list; <see cref="ResolveExact"/> decides when to fire a command
/// without waiting for Enter: only when the prefix is itself a complete mnemonic and no other
/// mnemonic shares it as a prefix. Because the <see cref="Abstractions.CommandMnemonics"/>
/// table is prefix-free, "exactly one match and it equals the prefix" captures that precisely.
/// without waiting for Enter: only when the prefix is itself a complete mnemonic and no longer
/// mnemonic starts with it. One that does (<c>q</c> under <c>qi</c>) runs on Enter (<see cref="Complete"/>).
/// </summary>
public sealed class MnemonicResolver
{
private readonly IReadOnlyList<MnemonicEntry> _entries;
private readonly IReadOnlyList<string> _every;

public MnemonicResolver(IEnumerable<MnemonicEntry> entries)
/// <param name="every">Every mnemonic, in scope or not, so hiding a longer one never makes a shorter one fire early.</param>
public MnemonicResolver(IEnumerable<MnemonicEntry> entries, IEnumerable<string>? every = null)
{
ArgumentNullException.ThrowIfNull(entries);
_entries = entries.OrderBy(e => e.Mnemonic, StringComparer.Ordinal).ToList();
_every = [.. (every ?? []).Concat(_entries.Select(e => e.Mnemonic))];
}

/// <summary>Every entry, ordered by mnemonic — the dialog's initial (unfiltered) list.</summary>
Expand All @@ -37,10 +39,12 @@ public IReadOnlyList<MnemonicEntry> Matching(string prefix) =>
/// matches nothing, or is still an incomplete/ambiguous lead-in (e.g. <c>c</c> while
/// only <c>cf</c> exists, or <c>f</c> with fe/ft/f1… beneath it).
/// </summary>
public MnemonicEntry? ResolveExact(string prefix)
{
if (string.IsNullOrEmpty(prefix)) return null;
var matches = Matching(prefix);
return matches.Count == 1 && matches[0].Mnemonic == prefix ? matches[0] : null;
}
public MnemonicEntry? ResolveExact(string prefix) =>
Complete(prefix) is { } entry && !_every.Any(m => m.Length > prefix.Length && m.StartsWith(prefix, StringComparison.Ordinal))
? entry
: null;

/// <summary>The entry whose mnemonic is exactly <paramref name="prefix"/>, whether or not a longer one starts with it.</summary>
public MnemonicEntry? Complete(string prefix) =>
string.IsNullOrEmpty(prefix) ? null : _entries.FirstOrDefault(e => e.Mnemonic == prefix);
}
14 changes: 7 additions & 7 deletions src/TuiCode.Workbench/Mnemonics/MnemonicView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ namespace TuiCode.Workbench.Mnemonics;
/// hands it that set, so a command out of scope isn't listed, can't be reached, and can't capture a
/// keystroke (#285). As the user types, the list filters to mnemonics with the typed prefix and the
/// command fires the instant the prefix is unambiguous — no Enter needed (see
/// <see cref="MnemonicResolver.ResolveExact"/>). Esc cancels; Backspace edits.
/// <see cref="MnemonicResolver.ResolveExact"/>). Enter runs a complete mnemonic a longer one starts with
/// (<c>q</c> under <c>qi</c>). Esc cancels; Backspace edits.
///
/// Unlike <see cref="Actions.ActionView"/> this captures raw keystrokes via a
/// <see cref="KeyCaptureScope"/> rather than a focused TextField. That keeps execution on the
Expand All @@ -37,12 +38,12 @@ public sealed class MnemonicView : Window
/// <summary>Fired after the view wants to be removed (Esc, or after a command was dispatched).</summary>
public event EventHandler? Closed;

public MnemonicView(IEnumerable<MnemonicEntry> entries, Action<string> execute)
public MnemonicView(IEnumerable<MnemonicEntry> entries, Action<string> execute, IEnumerable<string>? every = null)
{
ArgumentNullException.ThrowIfNull(entries);
ArgumentNullException.ThrowIfNull(execute);

_resolver = new MnemonicResolver(entries);
_resolver = new MnemonicResolver(entries, every);
_execute = execute;
_scope = new KeyCaptureScope(OnKey);

Expand Down Expand Up @@ -95,12 +96,11 @@ private void OnKey(Key key)
return;
}

// Enter commits the lone remaining match. Useful for a reserved family with a single
// member today (e.g. "c" → "cf"): you needn't type the second key once it's unambiguous.
// Enter runs the complete mnemonic typed, else the lone remaining match ("c" → "cf").
if (key == Key.Enter)
{
if (_visible.Count == 1)
Execute(_visible[0]);
if ((_resolver.Complete(_input.ToString()) ?? (_visible.Count == 1 ? _visible[0] : null)) is { } entry)
Execute(entry);
return;
}

Expand Down
6 changes: 3 additions & 3 deletions src/TuiCode.Workbench/Navigation/DefinitionPickerView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ public string Display(int placeWidth, int width)
}
}

/// <summary>Picks one of several definitions, such as a partial class's; the host does the move once it closes.</summary>
/// <summary>Picks one of several definitions or implementations, such as a partial class's; the host does the move once it closes.</summary>
public sealed class DefinitionPickerView : PickerDialog<DefinitionRow>
{
private const int DialogWidth = 66;

public DefinitionPickerView(string symbol, IReadOnlyList<DefinitionRow> rows)
: base($"Definitions of {symbol}", DialogWidth, Math.Min(18, rows.Count + 4), rows, r => $"{r.Place} {r.Code}", "Go to",
public DefinitionPickerView(string title, IReadOnlyList<DefinitionRow> rows)
: base(title, DialogWidth, Math.Min(18, rows.Count + 4), rows, r => $"{r.Place} {r.Code}", "Go to",
r => r.Display(rows.Max(d => d.Place.Length), PickerList.Width(DialogWidth)))
{
Commands.Register(CancelId, "Cancel", ClearOrCancel);
Expand Down
Loading
Loading