Add syntax highlighting and LSP for Dockerfiles(#6905) (#7977)

Release Notes:

- Added Dockerfile syntax highlighting and LSP support

---------

Co-authored-by: Bryce Palmer <bpalmer@redhat.com>
Co-authored-by: Kirill Bulatov <mail4score@gmail.com>
This commit is contained in:
d1y 2024-02-19 02:44:54 +08:00 committed by GitHub
parent 8acd4d122e
commit 12b12ba17a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
9 changed files with 227 additions and 0 deletions

10
Cargo.lock generated
View File

@ -10091,6 +10091,15 @@ dependencies = [
"tree-sitter",
]
[[package]]
name = "tree-sitter-dockerfile"
version = "0.1.0"
source = "git+https://github.com/camdencheek/tree-sitter-dockerfile?rev=33e22c33bcdbfc33d42806ee84cfd0b1248cc392#33e22c33bcdbfc33d42806ee84cfd0b1248cc392"
dependencies = [
"cc",
"tree-sitter",
]
[[package]]
name = "tree-sitter-elixir"
version = "0.1.0"
@ -11954,6 +11963,7 @@ dependencies = [
"tree-sitter-clojure",
"tree-sitter-cpp",
"tree-sitter-css",
"tree-sitter-dockerfile",
"tree-sitter-elixir",
"tree-sitter-elm",
"tree-sitter-embedded-template",

View File

@ -231,6 +231,7 @@ tree-sitter-clojure = { git = "https://github.com/prcastro/tree-sitter-clojure",
tree-sitter-c-sharp = { git = "https://github.com/tree-sitter/tree-sitter-c-sharp", rev = "dd5e59721a5f8dae34604060833902b882023aaf" }
tree-sitter-cpp = { git = "https://github.com/tree-sitter/tree-sitter-cpp", rev = "f44509141e7e483323d2ec178f2d2e6c0fc041c1" }
tree-sitter-css = { git = "https://github.com/tree-sitter/tree-sitter-css", rev = "769203d0f9abe1a9a691ac2b9fe4bb4397a73c51" }
tree-sitter-dockerfile = { git = "https://github.com/camdencheek/tree-sitter-dockerfile", rev = "33e22c33bcdbfc33d42806ee84cfd0b1248cc392" }
tree-sitter-elixir = { git = "https://github.com/elixir-lang/tree-sitter-elixir", rev = "a2861e88a730287a60c11ea9299c033c7d076e30" }
tree-sitter-elm = { git = "https://github.com/elm-tooling/tree-sitter-elm", rev = "692c50c0b961364c40299e73c1306aecb5d20f40" }
tree-sitter-embedded-template = "0.20.0"

View File

@ -116,6 +116,7 @@ tree-sitter-c.workspace = true
tree-sitter-clojure.workspace = true
tree-sitter-cpp.workspace = true
tree-sitter-css.workspace = true
tree-sitter-dockerfile.workspace = true
tree-sitter-elixir.workspace = true
tree-sitter-elm.workspace = true
tree-sitter-embedded-template.workspace = true

View File

@ -15,6 +15,7 @@ mod clojure;
mod csharp;
mod css;
mod deno;
mod dockerfile;
mod elixir;
mod elm;
mod erlang;
@ -73,6 +74,7 @@ pub fn init(
("clojure", tree_sitter_clojure::language()),
("cpp", tree_sitter_cpp::language()),
("css", tree_sitter_css::language()),
("dockerfile", tree_sitter_dockerfile::language()),
("elixir", tree_sitter_elixir::language()),
("elm", tree_sitter_elm::language()),
(
@ -151,6 +153,13 @@ pub fn init(
],
);
language(
"dockerfile",
vec![Arc::new(dockerfile::DockerfileLspAdapter::new(
node_runtime.clone(),
))],
);
match &ElixirSettings::get(None, cx).lsp {
elixir::ElixirLspSetting::ElixirLs => language(
"elixir",

View File

@ -0,0 +1,124 @@
use anyhow::{anyhow, Result};
use async_trait::async_trait;
use futures::StreamExt;
use language::{LanguageServerName, LspAdapter, LspAdapterDelegate};
use lsp::LanguageServerBinary;
use node_runtime::NodeRuntime;
use smol::fs;
use std::{
any::Any,
ffi::OsString,
path::{Path, PathBuf},
sync::Arc,
};
use util::{async_maybe, ResultExt};
const SERVER_PATH: &'static str =
"node_modules/dockerfile-language-server-nodejs/bin/docker-langserver";
fn server_binary_arguments(server_path: &Path) -> Vec<OsString> {
vec![server_path.into(), "--stdio".into()]
}
pub struct DockerfileLspAdapter {
node: Arc<dyn NodeRuntime>,
}
impl DockerfileLspAdapter {
pub fn new(node: Arc<dyn NodeRuntime>) -> Self {
Self { node }
}
}
#[async_trait]
impl LspAdapter for DockerfileLspAdapter {
fn name(&self) -> LanguageServerName {
LanguageServerName("docker-langserver".into())
}
fn short_name(&self) -> &'static str {
"dockerfile"
}
async fn fetch_latest_server_version(
&self,
_: &dyn LspAdapterDelegate,
) -> Result<Box<dyn 'static + Send + Any>> {
Ok(Box::new(
self.node
.npm_package_latest_version("dockerfile-language-server-nodejs")
.await?,
) as Box<_>)
}
async fn fetch_server_binary(
&self,
version: Box<dyn 'static + Send + Any>,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Result<LanguageServerBinary> {
let version = version.downcast::<String>().unwrap();
let server_path = container_dir.join(SERVER_PATH);
if fs::metadata(&server_path).await.is_err() {
self.node
.npm_install_packages(
&container_dir,
&[("dockerfile-language-server-nodejs", version.as_str())],
)
.await?;
}
Ok(LanguageServerBinary {
path: self.node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
}
async fn cached_server_binary(
&self,
container_dir: PathBuf,
_: &dyn LspAdapterDelegate,
) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir, &*self.node).await
}
async fn installation_test_binary(
&self,
container_dir: PathBuf,
) -> Option<LanguageServerBinary> {
get_cached_server_binary(container_dir, &*self.node).await
}
}
async fn get_cached_server_binary(
container_dir: PathBuf,
node: &dyn NodeRuntime,
) -> Option<LanguageServerBinary> {
async_maybe!({
let mut last_version_dir = None;
let mut entries = fs::read_dir(&container_dir).await?;
while let Some(entry) = entries.next().await {
let entry = entry?;
if entry.file_type().await?.is_dir() {
last_version_dir = Some(entry.path());
}
}
let last_version_dir = last_version_dir.ok_or_else(|| anyhow!("no cached binary"))?;
let server_path = last_version_dir.join(SERVER_PATH);
if server_path.exists() {
Ok(LanguageServerBinary {
path: node.binary_path().await?,
arguments: server_binary_arguments(&server_path),
})
} else {
Err(anyhow!(
"missing executable in directory {:?}",
last_version_dir
))
}
})
.await
.log_err()
}

View File

@ -0,0 +1,9 @@
name = "Dockerfile"
grammar = "dockerfile"
path_suffixes = ["Dockerfile"]
line_comments = ["# "]
brackets = [
{ start = "{", end = "}", close = true, newline = true },
{ start = "[", end = "]", close = true, newline = true },
{ start = "\"", end = "\"", close = true, newline = false, not_in = ["string"] },
]

View File

@ -0,0 +1,63 @@
; Dockerfile instructions set taken from:
; https://docs.docker.com/engine/reference/builder/#overview
; https://github.com/helix-editor/helix/blob/78c34194b5c83beb26ca04f12bf9d53fd5aba801/runtime/queries/dockerfile/highlights.scm
[
"ADD"
"ARG"
"CMD"
"COPY"
"ENTRYPOINT"
"ENV"
"EXPOSE"
"FROM"
"HEALTHCHECK"
"LABEL"
"MAINTAINER"
"ONBUILD"
"RUN"
"SHELL"
"STOPSIGNAL"
"USER"
"VOLUME"
"WORKDIR"
; "as" for multi-stage builds
"AS"
] @keyword
[
":"
"@"
] @operator
(comment) @comment
(image_spec
(image_tag
":" @punctuation.special)
(image_digest
"@" @punctuation.special))
[
(double_quoted_string)
(single_quoted_string)
(json_string)
] @string
[
(env_pair)
(label_pair)
] @constant
[
(param)
(mount_param)
] @function
(expansion
[
"$"
"{"
"}"
] @punctuation.special
) @constant

View File

@ -0,0 +1,6 @@
; We need impl this
; ((comment) @injection.content
; (#set! injection.language "comment"))
((shell_command) @content
(#set! "language" "bash"))

View File

@ -0,0 +1,4 @@
# Dockerfile
- Tree Sitter: [tree-sitter-dockerfile](https://github.com/camdencheek/tree-sitter-dockerfile)
- Language Server: [dockerfile-language-server](https://github.com/rcjsuen/dockerfile-language-server)