Skip to content

Commit 0c5dd8c

Browse files
committed
optimize document color algorithm
1 parent 467c12e commit 0c5dd8c

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
use schemars::JsonSchema;
2+
use serde::{Deserialize, Serialize};
3+
4+
#[derive(Serialize, Deserialize, Debug, JsonSchema)]
5+
#[serde(rename_all = "camelCase")]
6+
pub struct EmmyrcDocumentColor {
7+
/// Whether to enable document color.
8+
#[serde(default = "default_true")]
9+
pub enable: bool,
10+
}
11+
12+
impl Default for EmmyrcDocumentColor {
13+
fn default() -> Self {
14+
Self {
15+
enable: default_true(),
16+
}
17+
}
18+
}
19+
20+
fn default_true() -> bool {
21+
true
22+
}

crates/code_analysis/src/config/configs/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ mod strict;
1010
mod semantictoken;
1111
mod references;
1212
mod hover;
13+
mod document_color;
1314

1415

1516
pub use completion::{EmmyrcCompletion, EmmyrcFilenameConvention};
@@ -23,4 +24,5 @@ pub use codelen::EmmyrcCodeLen;
2324
pub use strict::EmmyrcStrict;
2425
pub use semantictoken::EmmyrcSemanticToken;
2526
pub use references::EmmyrcReference;
26-
pub use hover::EmmyrcHover;
27+
pub use hover::EmmyrcHover;
28+
pub use document_color::EmmyrcDocumentColor;

crates/code_analysis/src/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{collections::HashSet, path::PathBuf};
66

77
use crate::{semantic::LuaInferConfig, FileId};
88
pub use config_loader::load_configs;
9+
use configs::EmmyrcDocumentColor;
910
pub use configs::EmmyrcFilenameConvention;
1011
pub use configs::EmmyrcLuaVersion;
1112
use configs::{
@@ -49,6 +50,8 @@ pub struct Emmyrc {
4950
pub references: EmmyrcReference,
5051
#[serde(default)]
5152
pub hover: EmmyrcHover,
53+
#[serde(default)]
54+
pub document_color: EmmyrcDocumentColor,
5255
}
5356

5457
impl Emmyrc {

crates/emmylua_ls/src/handlers/document_color/build_color.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,16 @@ fn try_build_color_information(
3232
let mut i = 0;
3333
while i + 6 <= len {
3434
if bytes[i].is_ascii_hexdigit() {
35+
let is_start_boundary = if i == 0 {
36+
true
37+
} else {
38+
!bytes[i - 1].is_ascii_alphanumeric()
39+
};
40+
if !is_start_boundary {
41+
i += 1;
42+
continue;
43+
}
44+
3545
let mut j = i + 1;
3646
while j < len && bytes[j].is_ascii_hexdigit() {
3747
j += 1;
@@ -106,4 +116,4 @@ pub fn convert_color_to_hex(color: Color, len: usize) -> String {
106116
}
107117
_ => "".to_string(),
108118
}
109-
}
119+
}

crates/emmylua_ls/src/handlers/document_color/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ pub async fn on_document_color(
2929
return vec![];
3030
};
3131

32+
if !semantic_model.get_emmyrc().document_color.enable {
33+
return vec![];
34+
}
35+
3236
let document = semantic_model.get_document();
3337
let root = semantic_model.get_root();
3438
build_colors(root.syntax().clone(), &document)

resources/schema.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@
5050
}
5151
]
5252
},
53+
"documentColor": {
54+
"default": {
55+
"enable": true
56+
},
57+
"allOf": [
58+
{
59+
"$ref": "#/definitions/EmmyrcDocumentColor"
60+
}
61+
]
62+
},
5363
"hint": {
5464
"default": {
5565
"enable": true,
@@ -470,6 +480,16 @@
470480
}
471481
}
472482
},
483+
"EmmyrcDocumentColor": {
484+
"type": "object",
485+
"properties": {
486+
"enable": {
487+
"description": "Whether to enable document color.",
488+
"default": true,
489+
"type": "boolean"
490+
}
491+
}
492+
},
473493
"EmmyrcFilenameConvention": {
474494
"oneOf": [
475495
{

0 commit comments

Comments
 (0)