DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Model/
ModelOpen.rs1use std::sync::Arc;
11
12use serde_json::{Value, json};
13
14use crate::{
15 ApplicationState::DTO::DocumentStateDTO::DocumentStateDTO,
16 RunTime::ApplicationRunTime::ApplicationRunTime,
17};
18
19pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
20 let Uri = Arguments
21 .first()
22 .and_then(|V| V.as_str())
23 .ok_or("model:open requires uri".to_string())?
24 .to_owned();
25
26 let FilePath = if let Some(stripped) = Uri.strip_prefix("file://") {
27 stripped.to_owned()
28 } else {
29 Uri.clone()
30 };
31
32 let Content = tokio::fs::read_to_string(&FilePath)
33 .await
34 .map_err(|Error| format!("model:open read failed for {}: {}", FilePath, Error))?;
35
36 let LanguageId = std::path::Path::new(&FilePath)
37 .extension()
38 .and_then(|E| E.to_str())
39 .map(|Ext| {
40 match Ext {
41 "rs" => "rust",
42 "ts" | "tsx" => "typescript",
43 "js" | "jsx" | "mjs" | "cjs" => "javascript",
44 "json" | "jsonc" => "json",
45 "toml" => "toml",
46 "yaml" | "yml" => "yaml",
47 "md" => "markdown",
48 "html" | "htm" => "html",
49 "css" | "scss" | "less" => "css",
50 "sh" | "bash" | "zsh" => "shellscript",
51 "py" => "python",
52 "go" => "go",
53 "c" | "h" => "c",
54 "cpp" | "cc" | "cxx" | "hpp" => "cpp",
55 _ => "plaintext",
56 }
57 })
58 .unwrap_or("plaintext")
59 .to_owned();
60
61 let Version = RunTime
62 .Environment
63 .ApplicationState
64 .Feature
65 .Documents
66 .Get(&Uri)
67 .map(|D| D.Version + 1)
68 .unwrap_or(1);
69
70 if let Ok(ParsedUri) = url::Url::parse(&Uri) {
71 let Lines:Vec<String> = Content.lines().map(|L| L.to_owned()).collect();
72
73 let Eol = if Content.contains("\r\n") { "\r\n" } else { "\n" }.to_owned();
74
75 let Document = DocumentStateDTO {
76 URI:ParsedUri,
77
78 LanguageIdentifier:LanguageId.clone(),
79
80 Version,
81
82 Lines,
83
84 EOL:Eol,
85
86 IsDirty:false,
87
88 Encoding:"utf-8".to_owned(),
89
90 VersionIdentifier:Version,
91 };
92
93 RunTime
94 .Environment
95 .ApplicationState
96 .Feature
97 .Documents
98 .AddOrUpdate(Uri.clone(), Document);
99 }
100
101 RunTime
104 .Environment
105 .ApplicationState
106 .Workspace
107 .SetActiveDocumentURI(Some(Uri.clone()));
108
109 let NotifyUri = Uri.clone();
113
114 let NotifyLang = LanguageId.clone();
115
116 let NotifyVer = Version;
117
118 tokio::spawn(async move {
119 let _ = crate::Vine::Client::SendNotification::Fn(
120 "cocoon-main".to_string(),
121 "window.didChangeActiveTextEditor".to_string(),
122 serde_json::json!({ "uri": NotifyUri, "languageId": NotifyLang, "version": NotifyVer }),
123 )
124 .await;
125 });
126
127 Ok(json!({
128 "uri": Uri,
129 "content": Content,
130 "version": Version,
131 "languageId": LanguageId,
132 }))
133}