1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#![allow(unused_macros)]
// extern WASM calls are wrapped in unsafe,
// but they don't technically have to be.
#![deny(unused_unsafe)]

use anyhow::anyhow;
use external::{file_exists, is_absolute_path, mkdir, read_file, spawn, write_file};
use futures::lock::Mutex as AsyncMutex;
use hex::ToHex;
use indexmap::IndexMap;
use lsp_async_stub::{Context, Server};
use lsp_types::{notification, request, Url};
use schemars::{schema::RootSchema, schema_for};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{collections::HashMap, hash::Hash, path::Path, path::PathBuf, sync::Arc};
use taplo::{
    analytics::Directive,
    parser::Parse,
    schema::{CachedSchema, BUILTIN_SCHEME},
    util::coords::Mapper,
};

#[cfg(not(target_arch = "wasm32"))]
#[path = "external/native/mod.rs"]
#[macro_use]
pub mod external;

#[cfg(target_arch = "wasm32")]
#[path = "external/wasm32/mod.rs"]
#[macro_use]
pub mod external;

mod handlers;
mod msg_ext;
mod utils;

#[derive(Debug, Clone)]
pub struct Document {
    parse: Parse,
    mapper: Mapper,
}

/// Regex with hash and Eq
struct HashRegex(pub regex::Regex);

impl Hash for HashRegex {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.as_str().hash(state)
    }
}

impl PartialEq for HashRegex {
    fn eq(&self, other: &Self) -> bool {
        self.0.as_str() == other.0.as_str()
    }
}

impl Eq for HashRegex {}

impl From<regex::Regex> for HashRegex {
    fn from(re: regex::Regex) -> Self {
        Self(re)
    }
}

#[derive(Default)]
pub struct WorldState {
    cache_path: Option<PathBuf>,
    workspace_uri: Option<Url>,
    documents: HashMap<lsp_types::Url, Document>,
    schema_associations: IndexMap<HashRegex, String>,
    index_schema_associations: IndexMap<HashRegex, String>,
    http_client: reqwest::Client,
    configuration: Configuration,
    taplo_config: Option<taplo_cli::config::Config>,
}

impl WorldState {
    fn get_config_formatter_options(
        &self,
        uri: &Url,
        mut default_opts: taplo::formatter::Options,
    ) -> (
        taplo::formatter::Options,
        Vec<(String, taplo::formatter::OptionsIncomplete)>,
    ) {
        let mut incomplete = Vec::new();

        if let Some(c) = &self.taplo_config {
            if let Some(ws) = &self.workspace_uri {
                if let Some(p) = pathdiff::diff_paths(Path::new(uri.path()), ws.path()) {
                    if let Some(p) = p.to_str() {
                        match c.get_formatter_options(Some(p), Some(default_opts.clone())) {
                            Ok((opts, inc)) => {
                                default_opts = opts;
                                incomplete.extend(inc);
                            }
                            Err(err) => {
                                log_warn!("invalid config: {}", err);
                            }
                        }
                    }
                }
            }

            let p = uri.path();

            match c.get_formatter_options(Some(p), Some(default_opts.clone())) {
                Ok((opts, inc)) => {
                    default_opts = opts;
                    incomplete.extend(inc);
                }
                Err(err) => {
                    log_warn!("invalid config: {}", err);
                }
            }
        }

        (default_opts, incomplete)
    }

    fn get_schema_name(&self, uri: &Url) -> Option<String> {
        match self.documents.get(uri) {
            Some(doc) => {
                for directive in Directive::collect_from_syntax(doc.parse.clone().into_syntax()) {
                    if directive.value.starts_with("schema") {
                        return directive
                            .value
                            .split_whitespace()
                            .skip(1)
                            .next()
                            .map(|s| s.to_string());
                    }
                }
            }
            None => {}
        }

        if let Some(c) = &self.taplo_config {
            if let Some(ws) = &self.workspace_uri {
                if let Some(p) = pathdiff::diff_paths(Path::new(uri.path()), ws.path()) {
                    if let Some(p) = p.to_str() {
                        match c.get_schema_path(p) {
                            Ok(p) => {
                                if p.is_some() {
                                    return p;
                                }
                            }
                            Err(err) => {
                                log_warn!("invalid config: {}", err);
                            }
                        }
                    }
                }
            }

            let p = uri.path();

            match c.get_schema_path(p) {
                Ok(p) => {
                    if p.is_some() {
                        return p;
                    }
                }
                Err(err) => {
                    log_warn!("invalid config: {}", err);
                }
            }
        }

        let s = uri.as_str();

        for (re, name) in self.schema_associations.iter() {
            if re.0.is_match(s) {
                return Some(name.clone());
            }
        }

        for (re, name) in self.index_schema_associations.iter() {
            if re.0.is_match(s) {
                return Some(name.clone());
            }
        }

        None
    }

    fn workspace_path(&self) -> Option<PathBuf> {
        match &self.workspace_uri {
            Some(uri) => Some(PathBuf::from(uri.path())),
            None => None,
        }
    }

    fn workspace_absolute<P: AsRef<Path>>(&self, path: P) -> Option<PathBuf> {
        let workspace = &self.workspace_uri;

        match workspace {
            Some(uri) => Some(Path::new(uri.path()).join(path)),
            None => None,
        }
    }

    async fn get_schema(
        mut path: &str,
        mut context: Context<World>,
    ) -> Result<RootSchema, anyhow::Error> {
        if path.starts_with(&format!("{}://", BUILTIN_SCHEME)) {
            if path == "taplo://taplo.toml" {
                Ok(schema_for!(taplo_cli::config::Config))
            } else {
                Err(anyhow!("invalid builtin schema: {}", path))
            }
        } else if path.starts_with("http://") || path.starts_with("https://") {
            let w = context.world().lock().await;

            let mut hasher = Sha256::new();
            hasher.update(path.as_bytes());
            let url_hash = hasher.finalize().encode_hex::<String>();

            if let Some(cache_path) = &w.cache_path {
                let file_path = cache_path
                    .join("schemas")
                    .join(&url_hash)
                    .with_extension("json");
                let fp = file_path.to_str().unwrap();

                if file_exists(fp) {
                    let schema_bytes = read_file(fp).await?;
                    let cached_schema: CachedSchema = serde_json::from_slice(&schema_bytes)?;
                    return Ok(cached_schema.schema);
                }
            }

            let client = w.http_client.clone();
            drop(w);

            let res = client.get(path).send().await?;
            let schema: RootSchema = res.json().await.map_err::<anyhow::Error, _>(Into::into)?;

            let w = context.world().lock().await;

            let p = path.to_string();
            let s = schema.clone();

            // We also cache it here.
            if let Some(cache_path) = w.cache_path.clone() {
                match mkdir(cache_path.join("schemas").to_str().unwrap()) {
                    Ok(_) => {
                        spawn(async move {
                            if let Err(err) = write_file(
                                cache_path
                                    .join("schemas")
                                    .join(&url_hash)
                                    .with_extension("json")
                                    .to_str()
                                    .unwrap(),
                                &serde_json::to_vec(&CachedSchema {
                                    url: Some(p),
                                    schema: s,
                                })
                                .unwrap(),
                            )
                            .await
                            {
                                log_error!("failed to cache schema: {}", err);
                            };
                        });
                    }
                    Err(err) => {
                        log_error!("failed to cache schema: {}", err);
                    }
                }
            }

            Ok(schema)
        } else if path.starts_with("file://") {
            path = path.trim_start_matches("file://");
            serde_json::from_slice(&read_file(path).await?).map_err(Into::into)
        } else if is_absolute_path(path) {
            serde_json::from_slice(&read_file(path).await?).map_err(Into::into)
        } else {
            match context.world().lock().await.workspace_absolute(path) {
                Some(p) => serde_json::from_slice(&read_file(p.to_str().unwrap()).await?)
                    .map_err(Into::into),
                None => Err(anyhow!("cannot determine workspace root for relative path")),
            }
        }
    }
}

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SchemaConfiguration {
    pub enabled: Option<bool>,
    pub associations: Option<HashMap<String, String>>,
    pub repository_enabled: Option<bool>,
    pub repository_url: Option<String>,
    pub links: Option<bool>,
}

// This is not exhaustive
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Configuration {
    taplo_config: Option<String>,
    taplo_config_enabled: Option<bool>,
    schema: SchemaConfiguration,
    semantic_tokens: Option<bool>,
    cache_path: Option<String>,
    formatter: taplo::formatter::OptionsIncompleteCamel,
}

pub type World = Arc<AsyncMutex<WorldState>>;

pub fn create_server() -> Server<World> {
    Server::new()
        .on_request::<request::Initialize, _>(handlers::initialize)
        .on_request::<request::FoldingRangeRequest, _>(handlers::folding_ranges)
        .on_request::<request::DocumentSymbolRequest, _>(handlers::document_symbols)
        .on_request::<request::Formatting, _>(handlers::format)
        .on_request::<request::Completion, _>(handlers::completion)
        .on_request::<request::HoverRequest, _>(handlers::hover)
        .on_request::<request::DocumentLinkRequest, _>(handlers::links)
        .on_request::<request::SemanticTokensFullRequest, _>(handlers::semantic_tokens)
        .on_request::<request::CodeActionRequest, _>(handlers::code_action)
        .on_request::<msg_ext::TomlToJsonRequest, _>(handlers::toml_to_json)
        .on_request::<msg_ext::JsonToTomlRequest, _>(handlers::json_to_toml)
        .on_request::<msg_ext::SyntaxTreeRequest, _>(handlers::syntax_tree)
        .on_notification::<notification::DidOpenTextDocument, _>(handlers::document_open)
        .on_notification::<notification::DidChangeTextDocument, _>(handlers::document_change)
        .on_notification::<notification::DidCloseTextDocument, _>(handlers::document_close)
        .on_notification::<notification::DidChangeConfiguration, _>(handlers::configuration_change)
        .on_notification::<msg_ext::CachePath, _>(handlers::cache_path)
        .build()
}

pub fn create_world() -> World {
    Arc::new(AsyncMutex::new(WorldState::default()))
}