-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Micha Reiser <[email protected]>
- Loading branch information
1 parent
39ddad7
commit 729a4fc
Showing
15 changed files
with
3,907 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use std::fmt::{Display, Formatter}; | ||
use std::io; | ||
use std::num::NonZeroU16; | ||
use std::path::{Path, PathBuf}; | ||
use std::time::Instant; | ||
|
||
use anyhow::Result; | ||
use colored::Colorize; | ||
use rayon::iter::Either::{Left, Right}; | ||
use rayon::iter::{IntoParallelIterator, ParallelIterator}; | ||
use thiserror::Error; | ||
use tracing::metadata::Level; | ||
use tracing::{debug, warn}; | ||
use tracing_subscriber::filter::LevelFilter; | ||
use tracing_subscriber::layer::SubscriberExt; | ||
use tracing_subscriber::util::SubscriberInitExt; | ||
use tracing_subscriber::{Layer, Registry}; | ||
use tracing_tree::time::Uptime; | ||
|
||
use ruff::fs; | ||
use ruff::logging::LogLevel; | ||
use ruff::settings::types::PreviewMode; | ||
use ruff::warn_user_once; | ||
use ruff_formatter::LineWidth; | ||
use ruff_python_ast::{PySourceType, SourceType}; | ||
use ruff_python_formatter::{format_module, FormatModuleError, PyFormatOptions}; | ||
use ruff_source_file::{find_newline, LineEnding}; | ||
use ruff_workspace::resolver::python_files_in_path; | ||
|
||
use crate::args::{FormatArguments, LspArguments, LspCommand, Overrides}; | ||
use crate::resolve::resolve; | ||
use crate::ExitStatus; | ||
|
||
/// Format a set of files, and return the exit status. | ||
pub(crate) fn lsp(arguments: LspCommand, log_level: LogLevel) -> Result<ExitStatus> { | ||
let subscriber = Registry::default().with( | ||
tracing_tree::HierarchicalLayer::default() | ||
.with_indent_lines(true) | ||
.with_indent_amount(2) | ||
.with_bracketed_fields(true) | ||
.with_targets(true) | ||
.with_writer(|| Box::new(std::io::stderr())) | ||
.with_timer(Uptime::default()) | ||
.with_filter(LevelFilter::from(Some(Level::DEBUG))), | ||
); | ||
tracing::subscriber::with_default(subscriber, || { | ||
ruff_lsp::stdio(); | ||
}); | ||
|
||
Ok(ExitStatus::Success) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
[package] | ||
name = "ruff_lsp" | ||
version = "0.0.0" | ||
publish = false | ||
authors = { workspace = true } | ||
edition = { workspace = true } | ||
rust-version = { workspace = true } | ||
homepage = { workspace = true } | ||
documentation = { workspace = true } | ||
repository = { workspace = true } | ||
license = { workspace = true } | ||
|
||
[dependencies] | ||
ruff_linter = { path = "../ruff_linter" } | ||
ruff_cache = { path = "../ruff_cache" } | ||
ruff_diagnostics = { path = "../ruff_diagnostics" } | ||
ruff_formatter = { path = "../ruff_formatter" } | ||
ruff_notebook = { path = "../ruff_notebook" } | ||
ruff_macros = { path = "../ruff_macros" } | ||
ruff_python_ast = { path = "../ruff_python_ast" } | ||
ruff_python_formatter = { path = "../ruff_python_formatter" } | ||
ruff_source_file = { path = "../ruff_source_file" } | ||
ruff_python_trivia = { path = "../ruff_python_trivia" } | ||
ruff_workspace = { path = "../ruff_workspace" } | ||
ruff_text_size = { path = "../ruff_text_size" } | ||
|
||
tower-lsp = "0.20.0" | ||
tracing = {workspace = true} | ||
tokio = { version = "1.31.0", features = ["io-std", "rt", "rt-multi-thread"] } | ||
|
||
[dev-dependencies] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use ruff::RUFF_PKG_VERSION; | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use tower_lsp::jsonrpc::Result as LspResult; | ||
use tower_lsp::lsp_types::{ | ||
DidChangeTextDocumentParams, DidCloseTextDocumentParams, DidOpenTextDocumentParams, | ||
DocumentFormattingParams, InitializeParams, InitializeResult, InitializedParams, MessageType, | ||
PositionEncodingKind, ServerCapabilities, ServerInfo, TextDocumentSyncCapability, | ||
TextDocumentSyncKind, TextEdit, | ||
}; | ||
use tower_lsp::{Client, LanguageServer, LspService}; | ||
|
||
/// Creates a LSP server that reads from stdin and writes the output to stdout. | ||
pub fn stdio() { | ||
let mut rt = tokio::runtime::Runtime::new().unwrap(); | ||
rt.block_on(async { | ||
let stdin = tokio::io::stdin(); | ||
let stdout = tokio::io::stdout(); | ||
|
||
let (service, socket) = LspService::new(|client| Server { client }); | ||
tower_lsp::Server::new(stdin, stdout, socket) | ||
.serve(service) | ||
.await; | ||
}); | ||
} | ||
|
||
struct Server { | ||
client: Client, | ||
} | ||
|
||
#[tower_lsp::async_trait] | ||
impl LanguageServer for Server { | ||
#[tracing::instrument(level="debug", skip_all, err, fields(client=?params.client_info))] | ||
async fn initialize(&self, params: InitializeParams) -> LspResult<InitializeResult> { | ||
let init = InitializeResult { | ||
capabilities: ServerCapabilities { | ||
// TODO | ||
position_encoding: None, | ||
text_document_sync: Some(TextDocumentSyncCapability::Kind( | ||
TextDocumentSyncKind::INCREMENTAL, | ||
)), | ||
..Default::default() | ||
}, | ||
server_info: Some(ServerInfo { | ||
name: String::from(env!("CARGO_PKG_NAME")), | ||
version: Some(RUFF_PKG_VERSION.to_string()), | ||
}), | ||
}; | ||
|
||
Ok(init) | ||
} | ||
|
||
#[tracing::instrument(skip_all)] | ||
async fn initialized(&self, params: InitializedParams) {} | ||
|
||
#[tracing::instrument(skip_all, fields(file=%params.text_document.uri))] | ||
async fn did_open(&self, params: DidOpenTextDocumentParams) {} | ||
|
||
#[tracing::instrument(skip_all, fields(file=%params.text_document.uri))] | ||
async fn did_change(&self, params: DidChangeTextDocumentParams) {} | ||
|
||
#[tracing::instrument(skip_all, fields(file=%params.text_document.uri))] | ||
async fn did_close(&self, params: DidCloseTextDocumentParams) {} | ||
|
||
#[tracing::instrument(skip_all, fields(file=%params.text_document.uri))] | ||
async fn formatting( | ||
&self, | ||
params: DocumentFormattingParams, | ||
) -> LspResult<Option<Vec<TextEdit>>> { | ||
Ok(Some(vec![])) | ||
} | ||
|
||
#[tracing::instrument(skip_all, err)] | ||
async fn shutdown(&self) -> LspResult<()> { | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module.exports = { | ||
env: { | ||
es2021: true, | ||
node: true, | ||
}, | ||
extends: ["eslint:recommended", "plugin:@typescript-eslint/recommended"], | ||
overrides: [ | ||
{ | ||
env: { | ||
node: true, | ||
}, | ||
files: [".eslintrc.{js,cjs}"], | ||
parserOptions: { | ||
sourceType: "script", | ||
}, | ||
}, | ||
], | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
ecmaVersion: "latest", | ||
sourceType: "module", | ||
}, | ||
plugins: ["@typescript-eslint"], | ||
rules: { | ||
"@typescript-eslint/no-unused-vars": [ | ||
"error", | ||
{ argsIgnorePattern: "^_", varsIgnorePattern: "^_" }, | ||
], | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
out | ||
node_modules |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
** | ||
!icon.png | ||
!LICENSE | ||
!out/main.js | ||
!package.json | ||
!server/ruff | ||
!server/ruff.exe | ||
!README.md |
Oops, something went wrong.