|
| 1 | +//! Draws examples of Python diagnostics and completion from the settings window's draft. |
| 2 | +
|
| 3 | +use lithe_plugin_sdk::view::{ViewEvent, ViewEventKind, ViewResult, ViewSurface}; |
| 4 | +use lithe_plugin_sdk::{Context, FunctionError, PluginError}; |
| 5 | +use serde_json::{json, Value}; |
| 6 | + |
| 7 | +pub(crate) const FUNCTION: &str = "settings.preview"; |
| 8 | + |
| 9 | +pub(crate) fn draw(event: ViewEvent, context: &Context) -> Result<ViewResult, FunctionError> { |
| 10 | + context.check_cancelled()?; |
| 11 | + if !matches!(&event.surface, ViewSurface::SettingRegion(id) if id == "language") { |
| 12 | + return Err(PluginError::new( |
| 13 | + "python.preview.unknownRegion", |
| 14 | + "The Python settings preview requires the Python language region.", |
| 15 | + ) |
| 16 | + .into()); |
| 17 | + } |
| 18 | + if !matches!( |
| 19 | + event.kind, |
| 20 | + ViewEventKind::Open | ViewEventKind::Settings | ViewEventKind::Resize |
| 21 | + ) { |
| 22 | + return Ok(ViewResult::Unchanged); |
| 23 | + } |
| 24 | + Ok(ViewResult::View(vec![canvas(&event)])) |
| 25 | +} |
| 26 | + |
| 27 | +fn canvas(event: &ViewEvent) -> Value { |
| 28 | + let language = event.language.split('-').next() == Some("zh"); |
| 29 | + let words = |en, zh| if language { zh } else { en }; |
| 30 | + let value = |key| event.draft.get(key).or_else(|| event.effective.get(key)); |
| 31 | + let changed = [ |
| 32 | + "displayTypeErrors", |
| 33 | + "autoImportCompletions", |
| 34 | + "completeFunctionParens", |
| 35 | + "importFormat", |
| 36 | + ] |
| 37 | + .into_iter() |
| 38 | + .any(|key| { |
| 39 | + event |
| 40 | + .draft |
| 41 | + .get(key) |
| 42 | + .is_some_and(|draft| Some(draft) != event.effective.get(key)) |
| 43 | + }); |
| 44 | + let (diagnostic, tone) = match value("displayTypeErrors").and_then(Value::as_str) { |
| 45 | + Some("force-off") => ( |
| 46 | + words("Type errors hidden", "隐藏类型错误"), |
| 47 | + "$textSecondary", |
| 48 | + ), |
| 49 | + Some("error-missing-imports") => ( |
| 50 | + words("Missing imports only", "仅报告缺失导入"), |
| 51 | + "$textSecondary", |
| 52 | + ), |
| 53 | + Some("default") => ( |
| 54 | + words( |
| 55 | + "Depends on Pyrefly project configuration", |
| 56 | + "由项目的 Pyrefly 配置决定", |
| 57 | + ), |
| 58 | + "$textSecondary", |
| 59 | + ), |
| 60 | + _ => ( |
| 61 | + words( |
| 62 | + "Type error: expected int, got str", |
| 63 | + "类型错误:应为 int,实际为 str", |
| 64 | + ), |
| 65 | + "$error", |
| 66 | + ), |
| 67 | + }; |
| 68 | + let auto_import = value("autoImportCompletions") |
| 69 | + .and_then(Value::as_bool) |
| 70 | + .unwrap_or(true); |
| 71 | + let completion = if value("completeFunctionParens").and_then(Value::as_bool) == Some(true) { |
| 72 | + "greet()" |
| 73 | + } else { |
| 74 | + "greet" |
| 75 | + }; |
| 76 | + let import = if value("importFormat").and_then(Value::as_str) == Some("relative") { |
| 77 | + "from .utils import greet" |
| 78 | + } else { |
| 79 | + "from app.utils import greet" |
| 80 | + }; |
| 81 | + |
| 82 | + let width = event.width.unwrap_or(640.0).clamp(260.0, 4000.0); |
| 83 | + let gap = 12.0; |
| 84 | + let card_top = if changed { 53.0 } else { 30.0 }; |
| 85 | + let (card_width, second_x, second_y) = if width >= 570.0 { |
| 86 | + let card_width = (width - gap) / 2.0; |
| 87 | + (card_width, card_width + gap, card_top) |
| 88 | + } else { |
| 89 | + (width, 0.0, card_top + 90.0 + gap) |
| 90 | + }; |
| 91 | + let height = (second_y + 91.0) as u16; |
| 92 | + let title = words("Preview", "效果预览"); |
| 93 | + let mut nodes = vec![text(0.0, 17.0, width, title, "strong", "$text")]; |
| 94 | + if changed { |
| 95 | + nodes.push(text( |
| 96 | + 0.0, |
| 97 | + 39.0, |
| 98 | + width, |
| 99 | + words("Includes changes not applied yet", "含未应用的修改"), |
| 100 | + "caption", |
| 101 | + "$textSecondary", |
| 102 | + )); |
| 103 | + } |
| 104 | + nodes.push(card( |
| 105 | + 0.0, |
| 106 | + card_top, |
| 107 | + card_width, |
| 108 | + words("Type diagnostics", "类型诊断"), |
| 109 | + [ |
| 110 | + ("count: int = \"3\"", "monoSmall", "$text"), |
| 111 | + (diagnostic, "caption", tone), |
| 112 | + ], |
| 113 | + )); |
| 114 | + let first_line = if auto_import { |
| 115 | + (import, "monoSmall", "$text") |
| 116 | + } else { |
| 117 | + ( |
| 118 | + words("Auto-import off", "自动导入已关闭"), |
| 119 | + "caption", |
| 120 | + "$textSecondary", |
| 121 | + ) |
| 122 | + }; |
| 123 | + nodes.push(card( |
| 124 | + second_x, |
| 125 | + second_y, |
| 126 | + card_width, |
| 127 | + words("Code completion", "代码补全"), |
| 128 | + [first_line, (completion, "monoSmall", "$accent")], |
| 129 | + )); |
| 130 | + let mut result = json!({ |
| 131 | + "type": "canvas", "id": "pythonPreview", "label": words("Preview of Python settings", "Python 设置效果预览"), |
| 132 | + "height": height, "viewBox": [0, 0, width, height], "nodes": nodes, |
| 133 | + }); |
| 134 | + if event.width.is_some() { |
| 135 | + result["on"] = json!(["resize"]); |
| 136 | + } |
| 137 | + result |
| 138 | +} |
| 139 | + |
| 140 | +fn card(x: f64, y: f64, width: f64, title: &str, lines: [(&str, &str, &str); 2]) -> Value { |
| 141 | + let inner = width - 26.0; |
| 142 | + let [(first, first_style, first_color), (second, second_style, second_color)] = lines; |
| 143 | + json!({ |
| 144 | + "type": "group", "x": x, "y": y, "children": [ |
| 145 | + {"type": "rect", "x": 0.5, "y": 0.5, "w": width - 1.0, "h": 89, "radius": 8, "fill": "$surface", "stroke": "$line"}, |
| 146 | + text(13.0, 23.0, inner, title, "strong", "$text"), |
| 147 | + text(13.0, 49.0, inner, first, first_style, first_color), |
| 148 | + text(13.0, 72.0, inner, second, second_style, second_color), |
| 149 | + ], |
| 150 | + }) |
| 151 | +} |
| 152 | + |
| 153 | +fn text(x: f64, y: f64, width: f64, value: &str, style: &str, color: &str) -> Value { |
| 154 | + json!({"type": "text", "x": x, "y": y, "text": value, "style": style, "color": color, "maxWidth": width}) |
| 155 | +} |
0 commit comments