Skip to content

Commit 20d4b47

Browse files
committed
Extract the analyzers from VSCode extension.
This is probably more maintainable and adds the benefit of new analyzers like CFamily. However this is a paradigm shift from the current implementation. The current implementation downloads the analyzers when the corresponding parameter is enabled. In this new implementation, all servers are present and enabled out of the box. This makes things simpler, and removes the needs for subpackages for every analyzer. But this also means heavy change in the way this package is configured. Additionally, many sonarlint LSP extensions have been added/modified. I based my work on the source code of the typescript front end of vscode's extension.
1 parent a9b76eb commit 20d4b47

16 files changed

+367
-962
lines changed

README.md

Lines changed: 63 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,44 @@
66

77
SonarLint™ is a free IDE extension that lets you fix coding issues before they exist!
88

9-
Like a spell checker,it highlights Bugs and Security Vulnerabilities as you write code, with clear remediation guidance so you can fix them before the code is even committed.
9+
Like a spell checker,it highlights Bugs and Security Vulnerabilities as you write code, with clear remediation guidance so you can fix them before the code is even committed.
1010

11-
In Emacs supports analysis of JavaScript, TypeScript, Python, Java, HTML, PHP, Go, and XML out of the box!
11+
In Emacs supports analysis of JavaScript, TypeScript, Python, Java, HTML, PHP, C/C++,
12+
Go, and XML out of the box!
1213

1314
:warning: This is not an official [SonarSource](https://www.sonarsource.com/) extension
1415

1516
![Flycheck gif](https://gitlab.com/sasanidas/lsp-sonarlint/-/raw/master/examples/sonarlint-example.gif "Flycheck gif")
17+
18+
## How it works
19+
20+
lsp-sonarlint relies on the [official vscode extension](https://github.com/SonarSource/sonarlint-vscode/), which is downloaded and unzipped.
21+
22+
The official VSCode extension typically contains :
23+
- A language server (written in java)
24+
- A Java runtime
25+
- Analysers for 10+ languages
26+
- A VSCode front-end
27+
28+
lsp-sonarlint is emacs's equivalent of the VSCode front-end, but only
29+
implements the base feature set, i.e linting and rules viewing.
30+
1631
## Requirements
1732

1833
- emacs >= 25
1934
- [lsp-mode](https://github.com/emacs-lsp/lsp-mode)
2035
- [ht](https://github.com/Wilfred/ht.el)
2136
- [dash](https://github.com/magnars/dash.el)
2237

23-
The language server needs a Java Runtime (JRE) 8 or 11. If one is already installed on your computer, SonarLint should automatically find and use it.
24-
2538
To analyze JavaScript and TypeScript, SonarLint will also need Node.js.
2639

40+
To analyse C/C++ projects, SonarLint's CFamily analyzer will need both a working compiler and a
41+
compile_commands.json which is typically generated by [cmake](https://github.com/rizsotto/Bear) or [bear](https://github.com/rizsotto/Bear).
2742

2843
## Installation
2944

3045
## Source
31-
Download and include the main file lsp-sonarlint.el and the folders languages and server.
46+
Download and include the main file lsp-sonarlint.el.
3247

3348
### Melpa
3449
You can then run the following commands to install lsp-sonarlint:
@@ -40,51 +55,27 @@ M-x package-install RET lsp-sonarlint RET (to install and compile `lsp-sonarli
4055

4156
## Usage
4257
The language server relies on java plugins to properly analyze the selected language source code.
43-
The basic workflow to activate a plugin for a language is:
44-
45-
- Enable language specific extension, alongside lsp-sonarlint:
4658

47-
``` lisp
48-
(require 'lsp-sonarlint)
49-
(require 'lsp-sonarlint-LANGUAGENAME)
50-
```
51-
52-
- It is important to enable the desired languages **before** execute the lsp client:
59+
By default all are enabled, but you can specify the ones you want.
5360

5461
``` lisp
55-
(setq lsp-sonarlint-LANGUAGENAME-enabled t)
56-
```
57-
58-
- If the extension is not installed in the path **lsp-sonarlint-LANGUAGENAME-analyzer-path**, it will ask if you want to download it from
59-
the URL defined in **lsp-sonarlint-LANGUAGENAME-download-url**.
60-
61-
- If everything went well, you can now activate the *lsp* emacs command.
62-
63-
64-
#### Complete config example
65-
In this example, we have a multiple language project, with Javascript Typescript, HTML and PHP:
66-
67-
``` lisp
68-
(require 'lsp-sonarlint)
69-
70-
(require 'lsp-sonarlint-php)
71-
(setq lsp-sonarlint-php-enabled t)
72-
73-
(require 'lsp-sonarlint-html)
74-
(setq lsp-sonarlint-html-enabled t)
75-
76-
(require 'lsp-sonarlint-javascript)
77-
(setq lsp-sonarlint-javascript-enabled t)
78-
79-
(require 'lsp-sonarlint-typescript)
80-
(setq lsp-sonarlint-typescript-enabled t)
81-
```
82-
83-
Now we can activate the lsp extension.
84-
85-
The extension will check every plugin path and ask if it is not find to download it,
86-
the default path is defined in **lsp-sonarlint-LANGUAGENAME-analyzer-path**.
62+
(use-package lsp-sonarlint
63+
:custom
64+
;; Allow sonarlint to download and unzip the official VSCode extension
65+
;; If nil, you'll have to do that yourself. See also `lsp-sonarlint-download'
66+
;; `lsp-sonarlint-download-url' and `lsp-sonarlint-download-dir'
67+
(lsp-sonarlint-auto-download t)
68+
69+
;; Choose which analyzers you want enabled. By default all are enabled
70+
;; See command `lsp-sonarlint-available-analyzers' for the full list.
71+
(lsp-sonarlint-enabled-analyzers '("java" "cfamily" "python" "text")))
72+
```
8773

74+
> [!WARNING]
75+
> On windows, lsp-sonarlint may encounter [this issue](https://github.com/emacs-lsp/lsp-mode/issues/3022) while unzipping the VSCode extension. If you do, try the following :
76+
```elisp
77+
(setq lsp-unzip-script lsp-ext-pwsh-script)
78+
```
8879

8980
## Static Analysis Rules
9081

@@ -98,31 +89,48 @@ Out of the box, SonarLint automatically checks your code against the following r
9889
- [PHP rules](https://rules.sonarsource.com/php)
9990
- [XML rules](https://rules.sonarsource.com/xml)
10091
- [Go rules](https://rules.sonarsource.com/go)
92+
- [C rules](https://rules.sonarsource.com/c)
93+
- [C++ rules](https://rules.sonarsource.com/cpp)
10194
- [Bidi (bidirectional unicode characters)](https://rules.sonarsource.com/text/) + [Secrets](https://rules.sonarsource.com/secrets/)
10295

10396
## Supported settings
10497

105-
* `lsp-sonarlint-server-path` - Path of the sonarlint jar executable file.
98+
* `lsp-sonarlint-auto-download` - Set to t to enable auto-downloading of VSCode's extension on startup.
99+
* `lsp-sonarlint-download-url` - Specify another URL for the VSCode extension.
100+
* `lsp-sonarlint-download-dir` - Specify where VSCode's extension will be downloaded and unzipped.
101+
* `lsp-sonarlint-use-system-jre` - If t, use the system Java runtime instead of the bundled one.
102+
* `lsp-sonarlint-enabled-analyzers` - List of analyzers to enable. Defaults to 'all for all analyzers.
106103
* `lsp-sonarlint-modes-enabled` - List of major modes where the lsp server will activate.
107104
* `lsp-sonarlint-disable-telemetry` - Disable telemetry option (disabled by default).
108105
* `lsp-sonarlint-test-file-pattern` - Regex to find test file, most rules are not evaluated on test files.
109106
* `lsp-sonarlint-show-analyzer-logs` - Show analyzer logs.
110107
* `lsp-sonarlint-verbose-logs` - Make SonarLint logs verbose.
111-
* `lsp-sonarlint-server-download-url` - SonarLint server download URL.
112-
* `lsp-sonarlint-plugin-autodownload` - Not ask for confirmation and download analyzers if they are missing.
108+
* `lsp-sonarlint-cfamily-compilation-commands-path` - Path to compile_commands.json for C/C++ analysis.
109+
110+
### Available commands
111+
* `lsp-sonarlint-download` - Download the VSCode extension and unzip it. Called automatically
112+
if `lsp-sonarlint-auto-download`is set to t
113+
* `lsp-sonarlint-available-analyzers` - List all available analyzers provided by the downloaded VSCode extension.
113114

114-
### Plugins supported settings
115-
This settigns are common for all the language plugins.
115+
### Plugins additional info
116+
117+
For most analyzers, lsp-sonarlint provides variables describing additional
118+
info.
116119

117-
* `lsp-sonarlint-LANGUAGE-enabled` - Enable LANGUAGE lsp-sonarlint plugin (disable by default)
118-
* `lsp-sonarlint-LANGUAGE-download-url` - URL to download the LANGUAGE sonarlint plugin
119-
* `lsp-sonarlint-LANGUAGE-analyzer-path` - Location where the plugin/anlyzer is located.
120120
* `lsp-sonarlint-LANGUAGE-doc-url` - Sonarsource official plugin documentation
121121
* `lsp-sonarlint-LANGUAGE-repository-url` - Plugin source code
122122

123+
### Plugins not tested yet
124+
Currently, sonarlint's vscode extension also provides omnisharp and Infrastructure As Code (IAC)
125+
analyzers. They have not been tested yet, you may expect some additional configuration
126+
to make them work.
127+
You'll at least need to add the major-modes to `lsp-sonarlint-modes-enabled'.
128+
129+
Feel free to try them out and provide feedback.
130+
123131
## Data and telemetry
124132

125-
This extension collects anonymous usage data and sends it to SonarSource.
133+
This extension collects anonymous usage data and sends it to SonarSource.
126134

127135
Collection of telemetry is controlled via the setting: `lsp-sonarlint-disable-telemetry`, it is disable by default.
128136

fixtures/compile_commands.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
[
2+
{
3+
"directory": ".",
4+
"command": "/usr/bin/c++ sample.cpp",
5+
"file": "sample.cpp",
6+
"output": "dummy"
7+
}
8+
]

fixtures/sample.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int g(int& val)
2+
{
3+
return val;
4+
}

languages/go/lsp-sonarlint-go.el

Lines changed: 0 additions & 60 deletions
This file was deleted.

languages/html/lsp-sonarlint-html.el

Lines changed: 0 additions & 63 deletions
This file was deleted.

languages/java/lsp-sonarlint-java.el

Lines changed: 0 additions & 60 deletions
This file was deleted.

0 commit comments

Comments
 (0)