mirror of
https://github.com/tiennm99/serena.git
synced 2026-10-15 04:18:56 +00:00
Formatting, minor ruff fixes
This commit is contained in:
1 parent
6e2ed3969d
commit
3d4dba77bd
1 file changed
+31
-40
@@ -6,7 +6,6 @@ import logging
|
|||||||
import os
|
import os
|
||||||
import pathlib
|
import pathlib
|
||||||
import platform
|
import platform
|
||||||
import shlex
|
|
||||||
import shutil
|
import shutil
|
||||||
import stat
|
import stat
|
||||||
import subprocess
|
import subprocess
|
||||||
@@ -197,8 +196,7 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
|
|
||||||
if not nuget_cmd and not dotnet_cmd:
|
if not nuget_cmd and not dotnet_cmd:
|
||||||
raise LanguageServerException(
|
raise LanguageServerException(
|
||||||
"Neither nuget nor dotnet CLI is available. "
|
"Neither nuget nor dotnet CLI is available. " "Please install .NET SDK from https://dotnet.microsoft.com/download"
|
||||||
"Please install .NET SDK from https://dotnet.microsoft.com/download"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
@@ -216,7 +214,7 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
|
|
||||||
# Extract the nupkg (it's a zip file)
|
# Extract the nupkg (it's a zip file)
|
||||||
package_path = temp_path / f"{package_name}.{package_version}"
|
package_path = temp_path / f"{package_name}.{package_version}"
|
||||||
with zipfile.ZipFile(nupkg_path, 'r') as zip_ref:
|
with zipfile.ZipFile(nupkg_path, "r") as zip_ref:
|
||||||
zip_ref.extractall(package_path)
|
zip_ref.extractall(package_path)
|
||||||
|
|
||||||
logger.log("Successfully downloaded and extracted package", logging.INFO)
|
logger.log("Successfully downloaded and extracted package", logging.INFO)
|
||||||
@@ -227,7 +225,6 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
|
|
||||||
if package_path is None and dotnet_cmd:
|
if package_path is None and dotnet_cmd:
|
||||||
# Use dotnet restore to download the package
|
# Use dotnet restore to download the package
|
||||||
nuget_sources = " ".join([f"--source {src}" for src in runtime_deps["nugetSources"]])
|
|
||||||
project_content = f"""<Project Sdk="Microsoft.NET.Sdk">
|
project_content = f"""<Project Sdk="Microsoft.NET.Sdk">
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<TargetFramework>net9.0</TargetFramework>
|
<TargetFramework>net9.0</TargetFramework>
|
||||||
@@ -248,11 +245,18 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
try:
|
try:
|
||||||
# Use dotnet restore with no dependencies
|
# Use dotnet restore with no dependencies
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
[dotnet_cmd, "restore", str(project_file), "--packages", str(temp_path),
|
[
|
||||||
"--no-dependencies", "--ignore-failed-sources"],
|
dotnet_cmd,
|
||||||
|
"restore",
|
||||||
|
str(project_file),
|
||||||
|
"--packages",
|
||||||
|
str(temp_path),
|
||||||
|
"--no-dependencies",
|
||||||
|
"--ignore-failed-sources",
|
||||||
|
],
|
||||||
check=True,
|
check=True,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True
|
text=True,
|
||||||
)
|
)
|
||||||
package_path = temp_path / package_name.lower() / package_version
|
package_path = temp_path / package_name.lower() / package_version
|
||||||
|
|
||||||
@@ -266,14 +270,18 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
try:
|
try:
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
[
|
[
|
||||||
nuget_cmd, "install", package_name,
|
nuget_cmd,
|
||||||
"-Version", package_version,
|
"install",
|
||||||
"-OutputDirectory", str(temp_path),
|
package_name,
|
||||||
"-NonInteractive"
|
"-Version",
|
||||||
|
package_version,
|
||||||
|
"-OutputDirectory",
|
||||||
|
str(temp_path),
|
||||||
|
"-NonInteractive",
|
||||||
],
|
],
|
||||||
check=True,
|
check=True,
|
||||||
capture_output=True,
|
capture_output=True,
|
||||||
text=True
|
text=True,
|
||||||
)
|
)
|
||||||
|
|
||||||
# Find the downloaded package
|
# Find the downloaded package
|
||||||
@@ -294,16 +302,13 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
for possible_dir in [
|
for possible_dir in [
|
||||||
package_path / "tools" / "net9.0" / "any",
|
package_path / "tools" / "net9.0" / "any",
|
||||||
package_path / "lib" / "net9.0",
|
package_path / "lib" / "net9.0",
|
||||||
package_path / "contentFiles" / "any" / "net9.0"
|
package_path / "contentFiles" / "any" / "net9.0",
|
||||||
]:
|
]:
|
||||||
if possible_dir.exists():
|
if possible_dir.exists():
|
||||||
source_dir = possible_dir
|
source_dir = possible_dir
|
||||||
break
|
break
|
||||||
else:
|
else:
|
||||||
raise LanguageServerException(
|
raise LanguageServerException(f"Could not find language server files in package. " f"Searched in {package_path}")
|
||||||
f"Could not find language server files in package. "
|
|
||||||
f"Searched in {package_path}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Copy files to cache directory
|
# Copy files to cache directory
|
||||||
server_dir.mkdir(parents=True, exist_ok=True)
|
server_dir.mkdir(parents=True, exist_ok=True)
|
||||||
@@ -311,9 +316,7 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
shutil.copytree(source_dir, server_dir, dirs_exist_ok=True)
|
shutil.copytree(source_dir, server_dir, dirs_exist_ok=True)
|
||||||
|
|
||||||
if not server_dll.exists():
|
if not server_dll.exists():
|
||||||
raise LanguageServerException(
|
raise LanguageServerException("Microsoft.CodeAnalysis.LanguageServer.dll not found after extraction")
|
||||||
"Microsoft.CodeAnalysis.LanguageServer.dll not found after extraction"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Make the DLL executable on Unix-like systems
|
# Make the DLL executable on Unix-like systems
|
||||||
if system != "windows":
|
if system != "windows":
|
||||||
@@ -332,12 +335,7 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
if system_dotnet:
|
if system_dotnet:
|
||||||
# Check if it's .NET 9
|
# Check if it's .NET 9
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run([system_dotnet, "--list-runtimes"], capture_output=True, text=True, check=True)
|
||||||
[system_dotnet, "--list-runtimes"],
|
|
||||||
capture_output=True,
|
|
||||||
text=True,
|
|
||||||
check=True
|
|
||||||
)
|
|
||||||
if "Microsoft.NETCore.App 9." in result.stdout:
|
if "Microsoft.NETCore.App 9." in result.stdout:
|
||||||
logger.log("Found system .NET 9 runtime", logging.INFO)
|
logger.log("Found system .NET 9 runtime", logging.INFO)
|
||||||
return system_dotnet
|
return system_dotnet
|
||||||
@@ -373,11 +371,11 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
|
|
||||||
# Extract the archive
|
# Extract the archive
|
||||||
if archive_type == "zip":
|
if archive_type == "zip":
|
||||||
with zipfile.ZipFile(download_path, 'r') as zip_ref:
|
with zipfile.ZipFile(download_path, "r") as zip_ref:
|
||||||
zip_ref.extractall(dotnet_dir)
|
zip_ref.extractall(dotnet_dir)
|
||||||
else:
|
else:
|
||||||
# tar.gz
|
# tar.gz
|
||||||
with tarfile.open(download_path, 'r:gz') as tar_ref:
|
with tarfile.open(download_path, "r:gz") as tar_ref:
|
||||||
tar_ref.extractall(dotnet_dir)
|
tar_ref.extractall(dotnet_dir)
|
||||||
|
|
||||||
# Remove the archive
|
# Remove the archive
|
||||||
@@ -393,8 +391,6 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise LanguageServerException(f"Failed to download .NET 9 runtime from {url}: {e}") from e
|
raise LanguageServerException(f"Failed to download .NET 9 runtime from {url}: {e}") from e
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _get_initialize_params(self, repository_absolute_path: str) -> InitializeParams:
|
def _get_initialize_params(self, repository_absolute_path: str) -> InitializeParams:
|
||||||
"""
|
"""
|
||||||
Returns the initialize params for the Microsoft.CodeAnalysis.LanguageServer.
|
Returns the initialize params for the Microsoft.CodeAnalysis.LanguageServer.
|
||||||
@@ -446,12 +442,7 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
level = msg.get("type", 4) # Default to Log level
|
level = msg.get("type", 4) # Default to Log level
|
||||||
|
|
||||||
# Map LSP message types to Python logging levels
|
# Map LSP message types to Python logging levels
|
||||||
level_map = {
|
level_map = {1: logging.ERROR, 2: logging.WARNING, 3: logging.INFO, 4: logging.DEBUG} # Error # Warning # Info # Log
|
||||||
1: logging.ERROR, # Error
|
|
||||||
2: logging.WARNING, # Warning
|
|
||||||
3: logging.INFO, # Info
|
|
||||||
4: logging.DEBUG # Log
|
|
||||||
}
|
|
||||||
|
|
||||||
self.logger.log(f"LSP: {message_text}", level_map.get(level, logging.DEBUG))
|
self.logger.log(f"LSP: {message_text}", level_map.get(level, logging.DEBUG))
|
||||||
|
|
||||||
@@ -507,7 +498,7 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
section = item.get("section", "")
|
section = item.get("section", "")
|
||||||
|
|
||||||
# Provide default values based on the configuration section
|
# Provide default values based on the configuration section
|
||||||
if section.startswith("dotnet") or section.startswith("csharp"):
|
if section.startswith(("dotnet", "csharp")):
|
||||||
# Default configuration for C# settings
|
# Default configuration for C# settings
|
||||||
if "enable" in section or "show" in section or "suppress" in section or "navigate" in section:
|
if "enable" in section or "show" in section or "suppress" in section or "navigate" in section:
|
||||||
# Boolean settings
|
# Boolean settings
|
||||||
@@ -547,12 +538,12 @@ class CSharpLanguageServer(SolidLanguageServer):
|
|||||||
def handle_work_done_progress_create(params):
|
def handle_work_done_progress_create(params):
|
||||||
"""Handle work done progress create requests."""
|
"""Handle work done progress create requests."""
|
||||||
# Just acknowledge the request
|
# Just acknowledge the request
|
||||||
return None
|
return
|
||||||
|
|
||||||
def handle_register_capability(params):
|
def handle_register_capability(params):
|
||||||
"""Handle client/registerCapability requests."""
|
"""Handle client/registerCapability requests."""
|
||||||
# Just acknowledge the request - we don't need to track these for now
|
# Just acknowledge the request - we don't need to track these for now
|
||||||
return None
|
return
|
||||||
|
|
||||||
# Set up notification handlers
|
# Set up notification handlers
|
||||||
self.server.on_notification("window/logMessage", window_log_message)
|
self.server.on_notification("window/logMessage", window_log_message)
|
||||||
|
|||||||
Reference in new issue
Block a user