Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions ls/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ default = ["full-compiler"]
bitflags = { workspace = true }
dashmap = { version = "6.1.0" }
futures = "0.3.31"
regex = { workspace = true }
tracing = { version = "0.1.44", optional = true }
tracing-subscriber = { version = "0.3.22", optional = true }
itertools = { workspace = true}
Expand Down
55 changes: 44 additions & 11 deletions ls/editors/code/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# YARA-X for Visual Studio Code
# YARA for Visual Studio Code

This extension brings support for the [YARA](https://virustotal.github.io/yara-x/)
language to Visual Studio Code, powered by the official YARA-X Language Server. It
Expand Down Expand Up @@ -45,17 +45,50 @@ An image is worth a thousand words...

## Configuration

This extension respects the standard Visual Studio Code settings for formatting and other editor features. There are no YARA-X specific settings at this time.

The extension contributes the following default settings for the `[yara]` language:
This extension provides configurations through VSCode's configuration settings. All configurations are under `YARA.*`.

```json
"[yara]": {
"editor.tabSize": 4,
"editor.insertSpaces": false,
"editor.detectIndentation": false,
"editor.formatOnSave": true
}
"YARA.ruleNameValidation": "^APT_.+$",
"YARA.metadataValidation": [
{
"identifier": "author",
"required": true,
"type": "string"
},
{
"identifier": "version",
"required": true,
"type": "integer"
}
]
```

You can override these in your user or workspace settings.
### `YARA.ruleNameValidation`

Type: `string`
Default: `""` (no validation)

A regular expression that rule names must conform to. If a rule name does not match this pattern, a warning will be
generated.

### `YARA.metadataValidation`

Type: `array` of objects
Default: `[]` (no validation)

An array of objects, where each object defines validation rules for a specific metadata field. Each object can have
the following properties:

* `identifier` (string, required): The name of the metadata field to validate (e.g., `author`, `version`).
* `required` (boolean, optional): If `true`, the metadata field must be present in the rule. Defaults to `false`.
* `type` (string, optional): Specifies the expected type of the metadata value. Valid values are
`"string"`, `"integer"`, `"float"`, and `"bool"`. If the value does not match the specified type, a warning will
be generated.

For accessing these settings go to the Settings

<p align="center">
<img src="images/settings.gif" width="95%" alt="Demo">
<br/>
<em>(Demo)</em>
</p>
Binary file added ls/editors/code/images/settings.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions ls/editors/code/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "yara-x-ls",
"displayName": "YARA Language",
"description": "Rich support for the YARA language. Provides syntax highlighting, autocompletion and more.",
"description": "The official YARA language extension. Provides syntax highlighting, autocompletion and more.",
"version": "0.0.0",
"publisher": "VirusTotal",
"icon": "images/icon.png",
Expand Down Expand Up @@ -103,7 +103,7 @@
"YARA.ruleNameValidation": {
"type": "string",
"default": "",
"description": "A regular expression that all rule names must match."
"description": "A regular expression that all rule names must match (example: APT_.*)."
}
}
},
Expand Down
7 changes: 7 additions & 0 deletions ls/src/features/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,9 +483,16 @@ fn module_suggestions(
})
.collect()
} else {
let insert_text = match &ty {
Type::Array(_) => format!("{name}[${{1}}]${{2}}"),
_ => name.to_string(),
};

vec![CompletionItem {
label: name.to_string(),
kind: Some(CompletionItemKind::FIELD),
insert_text: Some(insert_text),
insert_text_format: Some(InsertTextFormat::SNIPPET),
label_details: Some(CompletionItemLabelDetails {
description: Some(ty_to_string(&ty)),
..Default::default()
Expand Down
21 changes: 17 additions & 4 deletions ls/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ use async_lsp::lsp_types::{
DocumentSymbolResponse, FullDocumentDiagnosticReport,
GotoDefinitionParams, GotoDefinitionResponse, Hover, HoverParams,
HoverProviderCapability, InitializeParams, InitializeResult, Location,
OneOf, PublishDiagnosticsParams, ReferenceParams,
MessageType, OneOf, PublishDiagnosticsParams, ReferenceParams,
RelatedFullDocumentDiagnosticReport, RenameParams, SaveOptions,
SelectionRange, SelectionRangeParams, SelectionRangeProviderCapability,
SemanticTokensFullOptions, SemanticTokensLegend, SemanticTokensOptions,
SemanticTokensRangeResult, SemanticTokensResult,
SemanticTokensServerCapabilities, ServerCapabilities,
SemanticTokensServerCapabilities, ServerCapabilities, ShowMessageParams,
TextDocumentSyncCapability, TextDocumentSyncKind, TextDocumentSyncOptions,
TextDocumentSyncSaveOptions, TextEdit, Url, WorkspaceEdit,
};
Expand Down Expand Up @@ -553,7 +553,7 @@ impl YARALanguageServer {
mut client: ClientSocket,
scope_uri: Url,
) -> Settings {
client
let settings = client
.configuration(ConfigurationParams {
items: vec![
ConfigurationItem {
Expand Down Expand Up @@ -583,7 +583,20 @@ impl YARALanguageServer {

Settings { metadata_validation, rule_name_validation }
})
.unwrap_or_default()
.unwrap_or_default();

if let Some(re) = &settings.rule_name_validation {
if regex::Regex::new(re).is_err() {
let _ = client.show_message(ShowMessageParams {
typ: MessageType::ERROR,
message: format!(
"YARA: wrong rule name validation regex: {re}"
),
});
}
}

settings
}

/// Sends diagnostics for specific document if publish model is used.
Expand Down
Loading
Loading