Skip to content

Commit

Permalink
Extract the analyzers from VSCode extension.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Horrih committed Jun 8, 2024
1 parent a9b76eb commit 20d4b47
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 962 deletions.
118 changes: 63 additions & 55 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,44 @@

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

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.
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.

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

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

![Flycheck gif](https://gitlab.com/sasanidas/lsp-sonarlint/-/raw/master/examples/sonarlint-example.gif "Flycheck gif")

## How it works

lsp-sonarlint relies on the [official vscode extension](https://github.com/SonarSource/sonarlint-vscode/), which is downloaded and unzipped.

The official VSCode extension typically contains :
- A language server (written in java)
- A Java runtime
- Analysers for 10+ languages
- A VSCode front-end

lsp-sonarlint is emacs's equivalent of the VSCode front-end, but only
implements the base feature set, i.e linting and rules viewing.

## Requirements

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

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.

To analyze JavaScript and TypeScript, SonarLint will also need Node.js.

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

## Installation

## Source
Download and include the main file lsp-sonarlint.el and the folders languages and server.
Download and include the main file lsp-sonarlint.el.

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

## Usage
The language server relies on java plugins to properly analyze the selected language source code.
The basic workflow to activate a plugin for a language is:

- Enable language specific extension, alongside lsp-sonarlint:

``` lisp
(require 'lsp-sonarlint)
(require 'lsp-sonarlint-LANGUAGENAME)
```

- It is important to enable the desired languages **before** execute the lsp client:
By default all are enabled, but you can specify the ones you want.

``` lisp
(setq lsp-sonarlint-LANGUAGENAME-enabled t)
```

- If the extension is not installed in the path **lsp-sonarlint-LANGUAGENAME-analyzer-path**, it will ask if you want to download it from
the URL defined in **lsp-sonarlint-LANGUAGENAME-download-url**.

- If everything went well, you can now activate the *lsp* emacs command.


#### Complete config example
In this example, we have a multiple language project, with Javascript Typescript, HTML and PHP:

``` lisp
(require 'lsp-sonarlint)
(require 'lsp-sonarlint-php)
(setq lsp-sonarlint-php-enabled t)
(require 'lsp-sonarlint-html)
(setq lsp-sonarlint-html-enabled t)
(require 'lsp-sonarlint-javascript)
(setq lsp-sonarlint-javascript-enabled t)
(require 'lsp-sonarlint-typescript)
(setq lsp-sonarlint-typescript-enabled t)
```

Now we can activate the lsp extension.

The extension will check every plugin path and ask if it is not find to download it,
the default path is defined in **lsp-sonarlint-LANGUAGENAME-analyzer-path**.
(use-package lsp-sonarlint
:custom
;; Allow sonarlint to download and unzip the official VSCode extension
;; If nil, you'll have to do that yourself. See also `lsp-sonarlint-download'
;; `lsp-sonarlint-download-url' and `lsp-sonarlint-download-dir'
(lsp-sonarlint-auto-download t)
;; Choose which analyzers you want enabled. By default all are enabled
;; See command `lsp-sonarlint-available-analyzers' for the full list.
(lsp-sonarlint-enabled-analyzers '("java" "cfamily" "python" "text")))
```

> [!WARNING]
> 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 :
```elisp
(setq lsp-unzip-script lsp-ext-pwsh-script)
```

## Static Analysis Rules

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

## Supported settings

* `lsp-sonarlint-server-path` - Path of the sonarlint jar executable file.
* `lsp-sonarlint-auto-download` - Set to t to enable auto-downloading of VSCode's extension on startup.
* `lsp-sonarlint-download-url` - Specify another URL for the VSCode extension.
* `lsp-sonarlint-download-dir` - Specify where VSCode's extension will be downloaded and unzipped.
* `lsp-sonarlint-use-system-jre` - If t, use the system Java runtime instead of the bundled one.
* `lsp-sonarlint-enabled-analyzers` - List of analyzers to enable. Defaults to 'all for all analyzers.
* `lsp-sonarlint-modes-enabled` - List of major modes where the lsp server will activate.
* `lsp-sonarlint-disable-telemetry` - Disable telemetry option (disabled by default).
* `lsp-sonarlint-test-file-pattern` - Regex to find test file, most rules are not evaluated on test files.
* `lsp-sonarlint-show-analyzer-logs` - Show analyzer logs.
* `lsp-sonarlint-verbose-logs` - Make SonarLint logs verbose.
* `lsp-sonarlint-server-download-url` - SonarLint server download URL.
* `lsp-sonarlint-plugin-autodownload` - Not ask for confirmation and download analyzers if they are missing.
* `lsp-sonarlint-cfamily-compilation-commands-path` - Path to compile_commands.json for C/C++ analysis.

### Available commands
* `lsp-sonarlint-download` - Download the VSCode extension and unzip it. Called automatically
if `lsp-sonarlint-auto-download`is set to t
* `lsp-sonarlint-available-analyzers` - List all available analyzers provided by the downloaded VSCode extension.

### Plugins supported settings
This settigns are common for all the language plugins.
### Plugins additional info

For most analyzers, lsp-sonarlint provides variables describing additional
info.

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

### Plugins not tested yet
Currently, sonarlint's vscode extension also provides omnisharp and Infrastructure As Code (IAC)
analyzers. They have not been tested yet, you may expect some additional configuration
to make them work.
You'll at least need to add the major-modes to `lsp-sonarlint-modes-enabled'.

Feel free to try them out and provide feedback.

## Data and telemetry

This extension collects anonymous usage data and sends it to SonarSource.
This extension collects anonymous usage data and sends it to SonarSource.

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

Expand Down
8 changes: 8 additions & 0 deletions fixtures/compile_commands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[
{
"directory": ".",
"command": "/usr/bin/c++ sample.cpp",
"file": "sample.cpp",
"output": "dummy"
}
]
4 changes: 4 additions & 0 deletions fixtures/sample.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
int g(int& val)
{
return val;
}
60 changes: 0 additions & 60 deletions languages/go/lsp-sonarlint-go.el

This file was deleted.

63 changes: 0 additions & 63 deletions languages/html/lsp-sonarlint-html.el

This file was deleted.

60 changes: 0 additions & 60 deletions languages/java/lsp-sonarlint-java.el

This file was deleted.

Loading

0 comments on commit 20d4b47

Please sign in to comment.