Skip to content

Commit 12d3739

Browse files
committed
refactor(editor): 删除 Core 的行注释符号表,注释符号由宿主语言注册表提供
editor.lineCommentToken 用一张写死的扩展名/语言 ID 表回答行注释符号, 违反 Core 对语言零特判的约束(Lithe-IDEA#1856 第 5 项)。宿主的语言注册表 (内置语言与插件 languages[].configuration.lineComment)已经声明每种语言的 注释,这张表是重复的第二份来源。 - 删除 editor.lineCommentToken 命令、LineCommentTokenRequest/LineCommentToken 类型和 line_comment_token 查表函数 - editor.lineEdit 不变,toggleLineComment 仍要求调用方传 commentToken - 共享夹具 line-edit-v1.json 删除 4 条 identifier 用例 - rust-core-api.md 删除命令说明,改写已过时的 macOS/Windows TypeScript 段落
1 parent 957a349 commit 12d3739

7 files changed

Lines changed: 19 additions & 208 deletions

File tree

‎rust/contracts/rust-core-api.md‎

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -650,7 +650,6 @@ or removing a command fails until the table changes in the same commit.
650650
| `editor.planTextEdits` | Validate and order a plugin document edit batch into UTF-16 offsets with the document's line ending |
651651
| `editor.parseNavigationInput` | Parse a bounded line or line:column input into zero-based editor coordinates |
652652
| `editor.navigationHistory.reduce` | Reduce one bounded back/forward navigation history event and return the next state plus the location to open |
653-
| `editor.lineCommentToken` | Resolve the line comment token for a file extension or language id |
654653
| `network.proxyPolicy` | Normalize the stored proxy settings into the policy a network stack must apply (mode, server, credentials, exclusion rules) |
655654
| `lsp.plainSnippet` | Convert LSP snippet insert text into plain editor text |
656655
| `lsp.builtinCompletions` | Return lightweight current-file identifier completions |
@@ -2400,7 +2399,7 @@ destination cannot be opened. See
24002399
`rust/fixtures/editor/navigation-history-v1.json`.
24012400

24022401
`editor.lineEdit` applies one deterministic line-level transform. All offsets
2403-
are UTF-16 code units so both hosts can feed the result straight into their
2402+
are UTF-16 code units so hosts can feed the result straight into their
24042403
text engines. The payload is `{ "operation": "toggleLineComment" |
24052404
"duplicateLine" | "deleteLine" | "moveLineUp" | "moveLineDown" | "copyLineUp" |
24062405
"copyLineDown", "source": string, "selectionStart": 0, "selectionLength": 0,
@@ -2414,16 +2413,10 @@ to restore. `applied: false` with every other field omitted marks a legitimate
24142413
no-op such as moving the first line up; callers must leave their text view
24152414
unchanged and must not consume the keyboard shortcut.
24162415

2417-
`editor.lineCommentToken` accepts `{ "identifier": "py" }` (case
2418-
insensitive; the identifier may be a file extension, a file name including
2419-
dotfiles such as `.env`, or a language id — extensions, dotfile basenames,
2420-
and language ids share one table) and returns `{ "token": "#" }` or
2421-
`{ "token": null }` for file types without a line comment token.
2422-
2423-
macOS consumes both commands today. Windows still uses its local TypeScript
2424-
implementations (`comment-toggle.ts`, `line-operations.ts`) and is expected
2425-
to adopt the same contract in a follow-up change; the fixture pins the
2426-
canonical behavior for that migration. Deterministic cases are pinned by
2416+
Core does not know which token a language uses: the host resolves
2417+
`commentToken` from its language registry, where every language — bundled or
2418+
contributed by a plugin through `languages[].configuration.lineComment` —
2419+
declares its own comments. Deterministic cases are pinned by
24272420
`rust/fixtures/editor/line-edit-v1.json`.
24282421

24292422
`network.proxyPolicy` normalizes the stored proxy settings into the policy a

‎rust/crates/core/src/protocol/command.rs‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,6 @@ pub enum EditorCommand {
608608
/// Reduces one bounded back/forward navigation history event
609609
/// (`editor.navigationHistory.reduce`).
610610
NavigationHistoryReduce,
611-
/// Resolves the line comment token for a file extension
612-
/// (`editor.lineCommentToken`).
613-
LineCommentToken,
614611
}
615612

616613
#[derive(Debug, Clone)]
@@ -1328,7 +1325,6 @@ impl CoreCommand {
13281325
"editor.navigationHistory.reduce" => {
13291326
Some(Self::Editor(EditorCommand::NavigationHistoryReduce))
13301327
}
1331-
"editor.lineCommentToken" => Some(Self::Editor(EditorCommand::LineCommentToken)),
13321328
"network.proxyPolicy" => Some(Self::NetworkProxyPolicy),
13331329
"lsp.plainSnippet" => Some(Self::Lsp(LspCommand::PlainSnippet)),
13341330
"lsp.builtinCompletions" => Some(Self::Lsp(LspCommand::BuiltinCompletions)),

‎rust/crates/core/src/runtime/dispatcher/editor.rs‎

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -63,23 +63,5 @@ pub(super) fn dispatch(
6363
Err(error) => CoreResponse::failure(id, error),
6464
}
6565
}
66-
EditorCommand::LineCommentToken => {
67-
match serde_json::from_value::<lithe_editor::LineCommentTokenRequest>(payload)
68-
.map_err(|error| {
69-
CoreError::new(
70-
ErrorCode::InvalidRequest,
71-
"Invalid editor line comment token request",
72-
)
73-
.with_details(error.to_string())
74-
})
75-
.map(lithe_editor::line_comment_token)
76-
{
77-
Ok(data) => CoreResponse::success(
78-
id,
79-
serde_json::to_value(data).expect("Comment token response should encode"),
80-
),
81-
Err(error) => CoreResponse::failure(id, error),
82-
}
83-
}
8466
}
8567
}

‎rust/crates/editor/src/lib.rs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
//! Editor commands owned by Rust Core: deterministic line edits and the
2-
//! comment-token table, find, plugin text edit plans, navigation input and
3-
//! history, editor preferences, recent files and crash-recovery decisions.
1+
//! Editor commands owned by Rust Core: deterministic line edits, find,
2+
//! plugin text edit plans, navigation input and history, editor
3+
//! preferences, recent files and crash-recovery decisions.
44
//!
55
//! Hosts apply the returned replacements and states through their own text
66
//! engine. The `editor.*` JSON commands are dispatched by `lithe-core`.
@@ -14,7 +14,7 @@ pub mod recent;
1414
pub mod recovery;
1515
pub mod text_edit_plan;
1616

17-
pub use line_edit::{line_comment_token, line_edit, LineCommentTokenRequest, LineEditRequest};
17+
pub use line_edit::{line_edit, LineEditRequest};
1818

1919
#[cfg(test)]
2020
mod tests;

‎rust/crates/editor/src/line_edit.rs‎

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,6 @@ pub struct LineEditRequest {
4949
pub comment_token: Option<String>,
5050
}
5151

52-
/// Request for `editor.lineCommentToken`.
53-
#[derive(Debug, Deserialize)]
54-
#[serde(rename_all = "camelCase")]
55-
pub struct LineCommentTokenRequest {
56-
/// File extension, file name (including dotfiles such as `.env`), or
57-
/// language id, matched case-insensitively.
58-
pub identifier: String,
59-
}
60-
6152
/// Result of `editor.lineEdit`. When `applied` is false the operation is a
6253
/// no-op (for example moving the first line up) and the caller must not
6354
/// modify its text view. Otherwise `text` replaces
@@ -115,53 +106,6 @@ impl LineEditOutcome {
115106
}
116107
}
117108

118-
/// Result of `editor.lineCommentToken`. `token` is `None` for file types
119-
/// without a line comment token; callers must not intercept the shortcut.
120-
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
121-
#[serde(rename_all = "camelCase")]
122-
pub struct LineCommentToken {
123-
/// Line comment token, such as `//` or `#`.
124-
pub token: Option<String>,
125-
}
126-
127-
/// Resolves the line comment token for a file extension, file name, or
128-
/// language id. The table is the single shared source of truth; it covers
129-
/// file extensions (`swift`, `py`), language ids (`python`, `javascript`),
130-
/// and dotfile names whose basename matches an entry (`.env`).
131-
pub fn line_comment_token(request: LineCommentTokenRequest) -> LineCommentToken {
132-
let key = request.identifier.to_lowercase();
133-
let token = lookup_line_comment_token(&key).or_else(|| {
134-
// Dotfiles report an empty pathExtension on macOS, so callers pass
135-
// the basename: fall back to the segment after the last dot so
136-
// `.env` resolves through `env` and `Foo.py` through `py`.
137-
let last_segment = key.rsplit('.').next()?;
138-
if last_segment.is_empty() || last_segment == key {
139-
return None;
140-
}
141-
lookup_line_comment_token(last_segment)
142-
});
143-
LineCommentToken {
144-
token: token.map(str::to_string),
145-
}
146-
}
147-
148-
fn lookup_line_comment_token(identifier: &str) -> Option<&'static str> {
149-
match identifier {
150-
// C family and other double-slash languages
151-
"c" | "h" | "cpp" | "cc" | "cxx" | "hpp" | "hxx" | "kt" | "kts" | "swift" | "js"
152-
| "jsx" | "mjs" | "cjs" | "ts" | "tsx" | "go" | "rs" | "cs" | "csharp" | "php" | "dart"
153-
| "scala" | "groovy" | "m" | "mm" | "zig" | "javascript" | "javascriptreact"
154-
| "typescript" | "typescriptreact" | "kotlin" | "rust" => Some("//"),
155-
// Hash languages
156-
"py" | "python" | "rb" | "ruby" | "sh" | "bash" | "zsh" | "shell" | "yml" | "yaml"
157-
| "toml" | "ini" | "cfg" | "conf" | "properties" | "env" | "dotenv" | "r" | "rmarkdown"
158-
| "pl" | "elixir" | "ex" => Some("#"),
159-
// Dash languages
160-
"sql" | "lua" | "hs" | "haskell" => Some("--"),
161-
_ => None,
162-
}
163-
}
164-
165109
/// Applies a line-level editing operation. Returns a no-op outcome instead of
166110
/// an error for legitimate boundary cases such as moving the first line up.
167111
pub fn line_edit(request: LineEditRequest) -> Result<LineEditOutcome, CoreError> {

‎rust/crates/editor/src/tests.rs‎

Lines changed: 10 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
//! Tests for the deterministic line-editing transforms and the comment
2-
//! token table. Expected values were pinned against IDEA semantics and the
3-
//! review regressions from PR #642.
1+
//! Tests for the deterministic line-editing transforms. Expected values
2+
//! were pinned against IDEA semantics and the review regressions from
3+
//! PR #642.
44
55
use serde_json::{json, Value};
66

7-
use super::line_edit::{line_comment_token, line_edit, LineCommentTokenRequest, LineEditRequest};
7+
use super::line_edit::{line_edit, LineEditRequest};
88

99
fn edit(
1010
operation: &str,
@@ -500,65 +500,6 @@ fn copy_line_down_behaves_like_duplicate() {
500500
assert_eq!(outcome["selectionStart"], json!(5));
501501
}
502502

503-
// MARK: Comment token table
504-
505-
#[test]
506-
fn line_comment_token_maps_common_extensions_and_language_ids() {
507-
for (extension, expected) in [
508-
("swift", "//"),
509-
("Swift", "//"),
510-
("ts", "//"),
511-
("csharp", "//"),
512-
("zig", "//"),
513-
("rust", "//"),
514-
("javascript", "//"),
515-
("javascriptreact", "//"),
516-
("typescript", "//"),
517-
("typescriptreact", "//"),
518-
("kotlin", "//"),
519-
("py", "#"),
520-
("python", "#"),
521-
("ruby", "#"),
522-
("yaml", "#"),
523-
("properties", "#"),
524-
("shell", "#"),
525-
("dotenv", "#"),
526-
("sql", "--"),
527-
("lua", "--"),
528-
] {
529-
let token = line_comment_token(LineCommentTokenRequest {
530-
identifier: extension.to_string(),
531-
});
532-
assert_eq!(
533-
token.token.as_deref(),
534-
Some(expected),
535-
"extension {extension}"
536-
);
537-
}
538-
}
539-
540-
#[test]
541-
fn line_comment_token_returns_none_for_unknown_extension() {
542-
for extension in ["json", ""] {
543-
let token = line_comment_token(LineCommentTokenRequest {
544-
identifier: extension.to_string(),
545-
});
546-
assert_eq!(token.token, None, "extension {extension}");
547-
}
548-
}
549-
550-
#[test]
551-
fn line_comment_token_resolves_dotfile_basenames() {
552-
// Regression: macOS pathExtension is empty for dotfiles such as .env,
553-
// so callers pass the basename and Core resolves the last dot segment.
554-
for (name, expected) in [(".env", Some("#")), ("Foo.py", Some("#")), ("README", None)] {
555-
let token = line_comment_token(LineCommentTokenRequest {
556-
identifier: name.to_string(),
557-
});
558-
assert_eq!(token.token.as_deref(), expected, "file name {name}");
559-
}
560-
}
561-
562503
#[test]
563504
fn move_down_maps_crlf_separator_caret_to_valid_selection() {
564505
// Regression: a caret between \r and \n once mapped to an offset
@@ -655,21 +596,12 @@ fn shared_fixture_cases_match_core_outcomes() {
655596
.expect("line edit fixture should be valid JSON");
656597
for case_value in fixture["cases"].as_array().expect("cases") {
657598
let name = case_value["name"].as_str().expect("case name");
658-
// The fixture covers both editor commands; dispatch on the request
659-
// shape so one file pins the whole editor contract.
660-
let outcome = if case_value["request"].get("operation").is_some() {
661-
let request: LineEditRequest = serde_json::from_value(case_value["request"].clone())
662-
.unwrap_or_else(|error| panic!("{name}: {error:?}"));
663-
serde_json::to_value(
664-
line_edit(request).unwrap_or_else(|error| panic!("{name}: {error:?}")),
665-
)
666-
.expect("outcome should serialize")
667-
} else {
668-
let request: LineCommentTokenRequest =
669-
serde_json::from_value(case_value["request"].clone())
670-
.unwrap_or_else(|error| panic!("{name}: {error:?}"));
671-
serde_json::to_value(line_comment_token(request)).expect("token should serialize")
672-
};
599+
let request: LineEditRequest = serde_json::from_value(case_value["request"].clone())
600+
.unwrap_or_else(|error| panic!("{name}: {error:?}"));
601+
let outcome = serde_json::to_value(
602+
line_edit(request).unwrap_or_else(|error| panic!("{name}: {error:?}")),
603+
)
604+
.expect("outcome should serialize");
673605
assert_eq!(outcome, case_value["expected"], "case {name}");
674606
}
675607
}

‎rust/fixtures/editor/line-edit-v1.json‎

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -356,24 +356,6 @@
356356
"selectionLength": 0
357357
}
358358
},
359-
{
360-
"name": "resolve line comment token for extension",
361-
"request": {
362-
"identifier": "py"
363-
},
364-
"expected": {
365-
"token": "#"
366-
}
367-
},
368-
{
369-
"name": "resolve line comment token for unknown extension",
370-
"request": {
371-
"identifier": "json"
372-
},
373-
"expected": {
374-
"token": null
375-
}
376-
},
377359
{
378360
"name": "move down normalizes a caret inside a CRLF separator",
379361
"request": {
@@ -391,15 +373,6 @@
391373
"selectionLength": 0
392374
}
393375
},
394-
{
395-
"name": "resolve line comment token for dotfile basename",
396-
"request": {
397-
"identifier": ".env"
398-
},
399-
"expected": {
400-
"token": "#"
401-
}
402-
},
403376
{
404377
"name": "duplicate preserves selection end across CRLF normalization",
405378
"request": {
@@ -416,15 +389,6 @@
416389
"selectionStart": 4,
417390
"selectionLength": 3
418391
}
419-
},
420-
{
421-
"name": "resolve line comment token for the rust language id",
422-
"request": {
423-
"identifier": "rust"
424-
},
425-
"expected": {
426-
"token": "//"
427-
}
428392
}
429393
]
430394
}

0 commit comments

Comments
 (0)