Skip to content

Commit 10da502

Browse files
1lckclaude
andauthored
feat(output-links): 运行控制台里的网址可点击 (#84)
* feat(output-links): 运行控制台里的网址可点击 outputLinks.match 在插件链接之后识别 http/https 网址,返回 {line,start,length,url}, 宿主在浏览器中打开。网址止于空白、引号、尖括号,去掉句末标点和未配对的右括号; 与插件链接重叠时插件链接优先。一行内的链接按起点排序。(1lck/Lithe-IDEA#1822) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * docs(plugins): 协议更新记录加入 #84 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 885f2a6 commit 10da502

5 files changed

Lines changed: 160 additions & 11 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ or removing a command fails until the table changes in the same commit.
510510
| `toolchains.instantiateLaunch` | Expand an enabled tool definition's `launch` template for a selected executable, entry, runtime arguments and arguments; a part the template cannot carry is `plugin.toolchain.launchMismatch`; performs no I/O |
511511
| `run.decodeStart` | Validate a plugin backend's `lithe/host/run/start` params against its manifest and the workspace, and return the normalized start with temporary-file paths substituted |
512512
| `debug.decodeBackendConfiguration` | Validate a plugin backend's `lithe/host/debug/start` params against its manifest and return the qualified adapter, request, label and configuration |
513-
| `outputLinks.match` | Find the links plugin `outputLinkMatchers` declare in Run or Test console lines, as UTF-16 ranges with candidate paths or a backend function to resolve them |
513+
| `outputLinks.match` | Find the links plugin `outputLinkMatchers` declare in Run or Test console lines, as UTF-16 ranges with candidate paths or a backend function to resolve them, and the web addresses the lines print, as `url` links the host opens in the browser |
514514
| `outputLinks.decodeResolution` | Check a matcher's `resolve` function reply and return the 1-based file location to open, or `null` |
515515
| `build.detect` | Find every build-system instance the enabled plugins declare in a workspace, with models that need no process |
516516
| `build.projectOptions` | Validate a native team's persisted Build options |

‎rust/crates/plugin-output-links/src/lib.rs‎

Lines changed: 115 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
//!
44
//! Core owns the whole decision. It checks declarations, finds matches in
55
//! console lines, and turns a match into a workspace location, or into the
6-
//! groups a plugin backend resolves. A host only shows links and opens them.
6+
//! groups a plugin backend resolves. It also links the web addresses a line
7+
//! prints, which no plugin declares, so a server's URL opens in the browser.
8+
//! A host only shows links and opens them.
79
810
use lithe_plugin_model::{invalid, Validation};
911
use lithe_protocol::{CoreError, ErrorCode};
@@ -208,12 +210,15 @@ fn request_error(message: &str) -> CoreError {
208210
}
209211

210212
/// `outputLinks.match`: finds links in console lines with the matchers of
211-
/// every installed plugin, in source order and then declaration order.
213+
/// every installed plugin, in source order and then declaration order, and
214+
/// then the web addresses the lines print.
212215
///
213-
/// Each link reports its line index, its UTF-16 `start` and `length`, the
214-
/// plugin and matcher, and either `location` with `paths` the host opens the
215-
/// first existing one of, or `resolve` with the groups the backend function
216-
/// receives. Matches overlapping an earlier link on the line are skipped.
216+
/// Each link reports its line index and its UTF-16 `start` and `length`. A
217+
/// plugin's link names the plugin and matcher and has either `location` with
218+
/// `paths` the host opens the first existing one of, or `resolve` with the
219+
/// groups the backend function receives; a web address has `url` alone, which
220+
/// the host opens in the browser. Matches overlapping an earlier link on the
221+
/// line are skipped, so a plugin's link wins over an address inside it.
217222
pub fn match_lines(payload: Value) -> Result<Value, CoreError> {
218223
let request: MatchRequest = serde_json::from_value(payload).map_err(|error| {
219224
request_error("Invalid output link match request.").with_details(error.to_string())
@@ -299,10 +304,71 @@ pub fn match_lines(payload: Value) -> Result<Value, CoreError> {
299304
links.push(link);
300305
}
301306
}
307+
for (start, end) in web_addresses(scanned) {
308+
if taken.len() >= MAX_LINKS_PER_LINE {
309+
break;
310+
}
311+
if taken
312+
.iter()
313+
.any(|(taken_start, taken_end)| start < *taken_end && *taken_start < end)
314+
{
315+
continue;
316+
}
317+
taken.push((start, end));
318+
links.push(json!({
319+
"line": line_index,
320+
"start": utf16_len(&scanned[..start]),
321+
"length": utf16_len(&scanned[start..end]),
322+
"url": &scanned[start..end],
323+
}));
324+
}
302325
}
326+
// A line's plugin links come first and its addresses after them, so the
327+
// links of a line are ordered by where they start.
328+
links.sort_by_key(|link| (link["line"].as_u64(), link["start"].as_u64()));
303329
Ok(json!({ "links": links }))
304330
}
305331

332+
/// The byte ranges of the `http` and `https` addresses in `line`. An address
333+
/// runs until whitespace or a character URLs do not contain unencoded; the
334+
/// punctuation that ends a sentence after it, and a closing bracket it did
335+
/// not open, are not part of it.
336+
fn web_addresses(line: &str) -> Vec<(usize, usize)> {
337+
static ADDRESS: OnceLock<Regex> = OnceLock::new();
338+
let address = ADDRESS.get_or_init(|| {
339+
Regex::new(r#"(?i)\bhttps?://[^\s<>"'`\x00-\x1f\x7f]+"#)
340+
.expect("the address pattern is valid")
341+
});
342+
let mut ranges = Vec::new();
343+
for found in address.find_iter(line) {
344+
let text = found.as_str();
345+
let mut end = text.len();
346+
while let Some(last) = text[..end].chars().next_back() {
347+
let unbalanced = |open: char, close: char| {
348+
last == close
349+
&& text[..end].matches(close).count() > text[..end].matches(open).count()
350+
};
351+
if ".,;:!?".contains(last)
352+
|| unbalanced('(', ')')
353+
|| unbalanced('[', ']')
354+
|| unbalanced('{', '}')
355+
{
356+
end -= last.len_utf8();
357+
} else {
358+
break;
359+
}
360+
}
361+
// An address needs a host after the scheme.
362+
if text[..end]
363+
.split_once("://")
364+
.is_some_and(|(_, rest)| !rest.is_empty())
365+
{
366+
ranges.push((found.start(), found.start() + end));
367+
}
368+
}
369+
ranges
370+
}
371+
306372
/// The longest prefix of `line` within `limit` bytes that ends on a
307373
/// character boundary.
308374
fn prefix(line: &str, limit: usize) -> &str {
@@ -444,6 +510,49 @@ pub fn decode_resolution(payload: Value) -> Result<Value, CoreError> {
444510
mod tests {
445511
use super::*;
446512

513+
fn addresses(line: &str) -> Vec<&str> {
514+
web_addresses(line)
515+
.into_iter()
516+
.map(|(start, end)| &line[start..end])
517+
.collect()
518+
}
519+
520+
#[test]
521+
fn web_addresses_end_before_trailing_punctuation_and_unopened_brackets() {
522+
assert_eq!(
523+
addresses("Local: http://localhost:8080/, see (https://example.org/a_(b)) now."),
524+
["http://localhost:8080/", "https://example.org/a_(b)"]
525+
);
526+
assert_eq!(
527+
addresses("Swagger UI: HTTP://127.0.0.1:8080/swagger-ui/index.html?x=1&y=[2]"),
528+
["HTTP://127.0.0.1:8080/swagger-ui/index.html?x=1&y=[2]"]
529+
);
530+
// Quotes, angle brackets and the scheme alone end or are no address.
531+
assert_eq!(
532+
addresses("<http://a.test/x> \"https://b.test\" http:// ftp://c.test mailto:x"),
533+
["http://a.test/x", "https://b.test"]
534+
);
535+
// A scheme inside a word is not the start of an address.
536+
assert!(addresses("nothttp://a.test").is_empty());
537+
}
538+
539+
#[test]
540+
fn a_plugin_link_wins_over_an_address_it_contains() {
541+
let reply = match_lines(json!({
542+
"scope": "run", "platform": "linux", "workspaceRoots": ["/w"],
543+
"sources": [{"pluginID": "p", "matchers": [{
544+
"id": "m", "pattern": r"see (?P<path>\S+) \S+", "resolve": {"function": "f"}
545+
}]}],
546+
"lines": ["see a.txt https://x.test/ then https://y.test/"]
547+
}))
548+
.unwrap();
549+
let links = reply["links"].as_array().unwrap();
550+
assert_eq!(links.len(), 2, "{links:?}");
551+
assert_eq!(links[0]["matcherID"], "m");
552+
assert_eq!(links[1]["url"], "https://y.test/");
553+
assert_eq!(links[1]["start"], 31);
554+
}
555+
447556
fn matcher(value: Value) -> OutputLinkMatcher {
448557
serde_json::from_value(value).unwrap()
449558
}

‎shared/plugins/contract/v1/README.md‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2743,9 +2743,15 @@ Core owns the decision, so every host links the same text:
27432743
line?, column?}`: the host opens the first existing path) or `resolve`
27442744
(`{function, groups}`). `start` and `length` count UTF-16 code units of the
27452745
line. Matches are found in source order and then declaration order, and a match
2746-
overlapping an earlier link is skipped. A request carries at most 256 lines,
2747-
64 sources and 16 roots; a line is scanned up to 4096 bytes and links at most
2748-
8 times.
2746+
overlapping an earlier link is skipped. Core then links the `http` and
2747+
`https` addresses a line prints, which no plugin declares: such a link is
2748+
`{line, start, length, url}` with no plugin, the host opens `url` in the
2749+
browser, and an address inside a plugin's link stays part of that link. An
2750+
address ends at whitespace, a quote or an angle bracket, without the
2751+
sentence punctuation after it or a closing bracket it did not open. The links
2752+
of a line are returned in the order they start. A request carries at most 256
2753+
lines, 64 sources and 16 roots; a line is scanned up to 4096 bytes and links
2754+
at most 8 times.
27492755
- `outputLinks.decodeResolution` takes `{reply}` (what the function answered)
27502756
and returns the location to open, `{path, line?, column?}` with 1-based
27512757
positions, or `null`. A reply opens only a `file:` URI and only the members

‎shared/plugins/contract/v1/fixtures/run/output-link-matchers.json‎

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
11
{
2-
"description": "Requests of outputLinks.match and the links Core returns, and replies of a matcher's resolve function with what outputLinks.decodeResolution returns. `start` and `length` count UTF-16 code units of the console line. A relative path yields one candidate per workspace root, and a path outside every root yields no link.",
2+
"description": "Requests of outputLinks.match and the links Core returns, and replies of a matcher's resolve function with what outputLinks.decodeResolution returns. `start` and `length` count UTF-16 code units of the console line. A relative path yields one candidate per workspace root, and a path outside every root yields no link. Web addresses are linked without a matcher, as `url` links, unless a plugin link covers them.",
33
"match": [
4+
{
5+
"name": "a web address links without a matcher and stops before trailing punctuation",
6+
"request": {
7+
"scope": "run",
8+
"platform": "macos",
9+
"workspaceRoots": ["/work/app"],
10+
"sources": [
11+
{
12+
"pluginID": "example.script",
13+
"matchers": [
14+
{ "id": "frame", "pattern": "File \"(?P<path>[^\"]+)\", line (?P<line>\\d+)" }
15+
]
16+
}
17+
],
18+
"lines": [
19+
"Tomcat started on http://localhost:8080/ (see https://example.org/docs_(v2)).",
20+
" File \"/work/app/main.script\", line 12, from https://example.org/x"
21+
]
22+
},
23+
"links": [
24+
{ "line": 0, "start": 18, "length": 22, "url": "http://localhost:8080/" },
25+
{ "line": 0, "start": 46, "length": 29, "url": "https://example.org/docs_(v2)" },
26+
{
27+
"line": 1,
28+
"start": 2,
29+
"length": 37,
30+
"pluginID": "example.script",
31+
"matcherID": "frame",
32+
"location": { "paths": ["/work/app/main.script"], "line": 12 }
33+
},
34+
{ "line": 1, "start": 46, "length": 21, "url": "https://example.org/x" }
35+
]
36+
},
437
{
538
"name": "a frame in the workspace links to its line",
639
"request": {

‎shared/plugins/reference/protocol-changelog.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@
138138

139139
| PR | 修改人 | API 编号 | 协议 / 能力 | 改了什么 |
140140
| --- | --- | --- | --- | --- |
141+
| [#84](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/84) | [@1lck](https://github.com/1lck) | — | `outputLinkMatchers` 0 内的补充 | 运行和测试控制台里的网址可点击([1lck/Lithe-IDEA#1822](https://github.com/1lck/Lithe-IDEA/issues/1822)):`outputLinks.match` 跑完插件匹配器后,再识别行内的 `http`/`https` 网址,返回不带插件的 `{line, start, length, url}`,宿主在浏览器中打开。网址止于空白、引号、尖括号,不含句末标点和未配对的右括号;和插件链接重叠时插件链接优先;一行内的链接按起点排序。fixture `output-link-matchers.json` 新增一例。清单格式和能力版本不变,插件无需改动;没有新增宿主方法或通知。 |
141142
| [#83](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/83) | [@xiaoyumuxi](https://github.com/xiaoyumuxi) | — | `languages` 0 内的补充 | 编辑器的 TextMate 引擎按 vscode-textmate 的方式运行语法的 `injections`([1lck/Lithe-IDEA#2112](https://github.com/1lck/Lithe-IDEA/issues/2112)):只有给文档分词的那个语法应用自己的 `injections`,被它包含的语法的不生效;某个位置的 scope 符合选择器时,注入规则和当前规则一起尝试,起点更靠前的匹配胜出,起点相同时当前规则胜出,除非选择器带 `L:` 前缀。Core 随之校验它:`injections` 是以 scope 选择器为键的规则对象,最多 64 项,选择器 1–1024 字节,每条规则和其他规则一样检查并计入 16384 条上限,违反时以 `plugin.grammar.invalid`(字段 `grammar.injections[.<选择器>…]`)拒绝。`injectionSelector` 仍不运行、不检查。清单格式和能力版本不变,不运行 `injections` 的旧宿主照常加载这些语法;没有新增宿主方法或通知。 |
142143
| [#79](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/79) | [@1lck](https://github.com/1lck) | API-19 | **`sourceSettings` 1** | 原生来源设置可以做成“项目结构”页([1lck/Lithe-IDEA#2086](https://github.com/1lck/Lithe-IDEA/issues/2086)):分组布局 `sidebar` 把来源列成左侧模块列表,来源用 `parent` 嵌套(同组、在前、最多 8 层);字段 `section` 在 `sidebar` 中是标签页、在 `form` 中是小标题;新字段类型 `entryList` 是有序的类型化条目列表,`entryKinds` 声明添加菜单(`file` 可带扩展名、`directory`、`choice` 带可选项),`attributes` 声明至多 4 个布尔或枚举列。条目的 `title`、`detail` 只用于显示,比较和写回时忽略;新增的 `choice` 条目必须来自当前可选项或原值。`SourceInput` 新增 `entries`,`effectiveDisplays` 对条目列表给出 `count`。快照 `version` 仍为 0;用到这些成员的插件声明 `sourceSettings: 1`,只支持 0 的宿主不会安装。新增 fixture `settings-sources-modules.json`。没有新增宿主方法或通知。 |
143144
| [#77](https://github.com/Lithe-IDEA/Lithe-core-rust/pull/77) | [@xiaoyumuxi](https://github.com/xiaoyumuxi) | API-29 | `buildSystems` 0 内的说明修正 | Core 不再内置运行发现探测器([1lck/Lithe-IDEA#1856](https://github.com/1lck/Lithe-IDEA/issues/1856) 第 4 项):原来按 `Cargo.toml`、`go.mod`、`package.json`、`pyproject.toml`、`Makefile`、`justfile`、`Procfile`、`compose.yaml` 自己生成的 Run 配置、指纹输入和工具链要求全部删除。Run 列表里自动生成的条目只来自已启用构建插件声明的 `buildSystems[].run`,模型何时过期只由插件的 `sync.watch` 决定。插件想让项目出现在 Run 列表里,就要声明 `run`。契约 README 只改了一句措辞(不再提“探测器生成的 Run 配置”);清单字段、校验、fixture 和能力版本都没有变,没有新增宿主方法或通知。 |

0 commit comments

Comments
 (0)