Skip to content

Commit 8d23494

Browse files
committed
[NEW] Zed extension
0 parents  commit 8d23494

File tree

8 files changed

+1779
-0
lines changed

8 files changed

+1779
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target/
2+
*.lock
3+
*.wasm

COPYRIGHT

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
Most of the files are
3+
4+
Copyright (c) 2004-2015 Odoo S.A.
5+
6+
Many files also contain contributions from third
7+
parties. In this case the original copyright of
8+
the contributions can be traced through the
9+
history of the source version control system.
10+
11+
When that is not the case, the files contain a prominent
12+
notice stating the original copyright and applicable
13+
license, or come with their own dedicated COPYRIGHT
14+
and/or LICENSE file.

Cargo.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[package]
2+
name = "odoo"
3+
version = "0.12.1"
4+
edition = "2021"
5+
6+
[lib]
7+
path = "src/odoo.rs"
8+
crate-type = ["cdylib"]
9+
10+
[dependencies]
11+
zed_extension_api = "0.6.0"

LICENSE

Lines changed: 859 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<h1 align="center">
2+
<br>
3+
<img src="https://github.com/odoo/odoo-vscode/blob/main/images/odoo_logo.png?raw=true"></a>
4+
<br>
5+
Odoo Zed Extension
6+
<br>
7+
</h1>
8+
9+
<h4 align="center">Boost your Odoo code development</h4>
10+
11+
## About
12+
13+
This extension integrates the Odoo Language Server, that will help you in the development of your Odoo projects.
14+
15+
This repository contains the code that build the Zed extension for OdooLS. OdooLs itself is available [here](https://github.com/odoo/odoo-ls)
16+
17+
## Settings
18+
19+
You can provide settings to the plugin:
20+
21+
```json
22+
"lsp": {
23+
"odoo": {
24+
"settings": {
25+
"Odoo": {
26+
"selectedProfile": "my_profile"
27+
}
28+
}
29+
}
30+
}
31+
```
32+
33+
## Limitations
34+
35+
Due to the lack of features of the Zed API, following options are not available on the Zed plugin:
36+
37+
- Profile selector. You can learn about the "default" profile [here](https://github.com/odoo/odoo-ls/wiki/3.-Configuration-files#no-configuration-file)
38+
39+
You can change the selected profile in your settings though
40+
- Configuration view and aggregator
41+
- Status widget
42+
- Crash report view

changelog.md

Lines changed: 673 additions & 0 deletions
Large diffs are not rendered by default.

extension.toml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
id = "odoo"
2+
name = "Odoo"
3+
version = "0.12.1"
4+
schema_version = 1
5+
authors = ["Odoo<[email protected]>"]
6+
description = "This extension integrates the Odoo Language Server, that will help you in the development of your Odoo projects."
7+
repository = "https://github.com/odoo/odoo-zed"
8+
9+
[language_servers.odoo]
10+
name = "Odoo"
11+
languages = ["Python", "HTML", "XML", "CSV"]
12+
13+
[language-servers.odoo.language_ids]
14+
"Python" = "python"
15+
"HTML" = "html"
16+
"CSS" = "css"
17+
"XML" = "xml"

src/odoo.rs

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
use std::{error::Error, fs::{self}, path::{Path}};
2+
3+
use zed_extension_api::{self as zed, settings::LspSettings, LanguageServerId};
4+
5+
pub const VERSION: &str = env!("CARGO_PKG_VERSION");
6+
7+
struct Odoo {
8+
cached_binary_path: Option<String>,
9+
}
10+
11+
impl Odoo {
12+
13+
fn language_server_binary_path(
14+
&mut self,
15+
language_server_id: &LanguageServerId,
16+
_worktree: &zed::Worktree,
17+
) -> Result<String, Box<dyn Error>> {
18+
19+
if let Some(path) = &self.cached_binary_path {
20+
if fs::metadata(path).is_ok_and(|stat| stat.is_file()) {
21+
return Ok(path.clone());
22+
}
23+
}
24+
25+
zed::set_language_server_installation_status(
26+
language_server_id,
27+
&zed::LanguageServerInstallationStatus::CheckingForUpdate,
28+
);
29+
30+
let version_dir = format!("{}", VERSION);
31+
fs::create_dir_all(version_dir.clone()).map_err(|err| format!("failed to create version directory: {err}"))?;
32+
33+
let release = zed::github_release_by_tag_name(
34+
"odoo/odoo-ls",
35+
VERSION,
36+
)?;
37+
38+
let asset_name = format!("odoo-{}-{}.zip", Odoo::platform(), VERSION);
39+
40+
let asset = release.assets.iter().find(|asset| asset.name == asset_name).ok_or_else(
41+
|| format!("Odoo: No asset found for asset name {}", asset_name)
42+
)?;
43+
44+
let asset_typeshed = release.assets.iter().find(|asset| asset.name == "typeshed.zip").ok_or_else(
45+
|| format!("Odoo: No asset found for asset name {}", "typeshed.zip")
46+
)?;
47+
48+
let mut exe_name = String::from("odoo_ls_server");
49+
if cfg!(windows) {
50+
exe_name += ".exe";
51+
}
52+
53+
let binary_path = format!(
54+
"{version_dir}/{bin_name}",
55+
bin_name = match zed::current_platform().0 {
56+
zed::Os::Windows => format!("{}.exe", "odoo_ls_server"),
57+
zed::Os::Mac | zed::Os::Linux => "odoo_ls_server".to_string(),
58+
}
59+
);
60+
61+
if !fs::metadata(&binary_path).is_ok_and(|stat| stat.is_file()) {
62+
zed::set_language_server_installation_status(
63+
language_server_id,
64+
&zed::LanguageServerInstallationStatus::Downloading,
65+
);
66+
67+
let path_typeshed = Path::new(&version_dir).join("typeshed");
68+
if path_typeshed.exists() {
69+
fs::remove_dir_all(path_typeshed)?;
70+
}
71+
72+
zed::download_file(&asset.download_url, &version_dir, zed::DownloadedFileType::Zip)
73+
.map_err(|err| format!("failed to download file: {err}"))?;
74+
75+
zed::download_file(&asset_typeshed.download_url, &version_dir, zed::DownloadedFileType::Zip)
76+
.map_err(|err| format!("failed to download file: {err}"))?;
77+
78+
zed::make_file_executable(&binary_path)?;
79+
80+
let entries = fs::read_dir(".")
81+
.map_err(|err| format!("failed to list working directory {err}"))?;
82+
for entry in entries {
83+
let entry = entry.map_err(|err| format!("failed to load directory entry {err}"))?;
84+
if entry.file_name().to_str() != Some(&version_dir) {
85+
fs::remove_dir_all(entry.path()).ok();
86+
}
87+
}
88+
}
89+
90+
self.cached_binary_path = Some(binary_path.clone());
91+
92+
Ok(binary_path)
93+
}
94+
95+
fn platform() -> &'static str {
96+
let (platform, arch) = zed::current_platform();
97+
match (platform, arch) {
98+
(zed::Os::Linux, zed::Architecture::X8664) if cfg!(target_env = "musl") => "alpine-x64",
99+
(zed::Os::Linux, zed::Architecture::Aarch64) if cfg!(target_env = "musl") => "alpine-arm64",
100+
(zed::Os::Linux, zed::Architecture::X8664) => "linux-x64",
101+
(zed::Os::Linux, zed::Architecture::Aarch64) => "linux-arm64",
102+
(zed::Os::Windows, zed::Architecture::X8664) => "win32-x64",
103+
(zed::Os::Windows, zed::Architecture::Aarch64) => "win32-arm64",
104+
(zed::Os::Mac, zed::Architecture::X8664) => "darwin-x64",
105+
(zed::Os::Mac, zed::Architecture::Aarch64) => "darwin-arm64",
106+
(_os, arch) => {
107+
// fallback
108+
println!("Odoo: Warning: Unsupported platform {platform:?}-{arch:?}");
109+
Box::leak(format!("unknown").into_boxed_str())
110+
}
111+
}
112+
}
113+
}
114+
115+
impl zed::Extension for Odoo {
116+
117+
fn new() -> Self {
118+
Self {
119+
cached_binary_path: None
120+
}
121+
}
122+
123+
fn language_server_command(
124+
&mut self,
125+
language_server_id: &LanguageServerId,
126+
worktree: &zed::Worktree,
127+
) -> Result<zed::Command, String> {
128+
Ok(zed::Command {
129+
command: self.language_server_binary_path(language_server_id, worktree).map_err(|e| e.to_string())?,
130+
args: vec![],
131+
env: vec![],
132+
})
133+
}
134+
135+
fn language_server_initialization_options(
136+
&mut self,
137+
server_id: &LanguageServerId,
138+
worktree: &zed_extension_api::Worktree,
139+
) -> Result<Option<zed_extension_api::serde_json::Value>, String> {
140+
let settings = LspSettings::for_worktree(server_id.as_ref(), worktree)
141+
.ok()
142+
.and_then(|lsp_settings| lsp_settings.initialization_options.clone())
143+
.unwrap_or_default();
144+
Ok(Some(settings))
145+
}
146+
147+
fn language_server_workspace_configuration(
148+
&mut self,
149+
server_id: &LanguageServerId,
150+
worktree: &zed_extension_api::Worktree,
151+
) -> Result<Option<zed_extension_api::serde_json::Value>, String> {
152+
let settings = LspSettings::for_worktree(server_id.as_ref(), worktree)
153+
.ok()
154+
.and_then(|lsp_settings| lsp_settings.settings.clone())
155+
.unwrap_or_default();
156+
Ok(Some(settings))
157+
}
158+
}
159+
160+
zed::register_extension!(Odoo);

0 commit comments

Comments
 (0)