How omnisharp/omnisharp-server works
Project type and purpose
• This is an open-source language server / HTTP wrapper around the NRefactory C# parser.
• It exposes REST endpoints (via NancyFx) to editor plugins (Vim, Sublime, Emacs, Atom, YCM).
• It orchestrates solution/project loading, code parsing, analysis, refactoring, build and code-action operations.
File structure
Top-level folders:
• OmniSharp — main C# source (handlers/modules, core services, domain models)
• OmniSharp.Tests — feature-by-feature automated tests
• Microsoft.Build.Evaluation — MSBuild project evaluation library
• NRefactory (git submodule) — C# parser/AST generator
• packages — third-party NuGet libraries (NancyFx, Mono.Cecil, NUnit, etc.)
Configuration files: config.json, app.config, .sln, .csproj.
Extract README insights
• The “Bootstrapper” spins up a Nancy self-host on a TCP port.
• Each feature (autocomplete, build, code format, find usages, etc.) is implemented as a Nancy module + handler + request/response DTOs.
• Metadata modules generate swagger docs for each endpoint.
Main components
A. HTTP/API Layer
– NancyFx Self-Host (Bootstrapper → NancyHost)
– Nancy Modules (one per feature folder, e.g., AutoCompleteModule, BuildModule, CodeFormatModule, etc.)
– Nancy Metadata Modules (Swagger metadata providers)
B. Handler Layer
– Feature Handlers (AutoCompleteHandler, BuildHandler, CodeFormatHandler, etc.)
– Each handler receives a Request DTO, coordinates services, returns a Response DTO.
C. Core Services & Domain Logic
– Request routing → Handler → Service classes (e.g., CompletionDataFactory, BuildCommandBuilder, DocumentationFetcher, QuickFixService)
– Parser layer (BufferParser, NRefactory AST via NRefactory submodule)
– Project/Solution abstraction (CSharpSolution, MsBuildWorkspaceInformation, ProjectFinder)
– File system abstraction (IFileSystem, FileSystem)
– Configuration (OmniSharpConfiguration, path replacement, test commands)
D. External Dependencies
– NRefactory (C# parsing & AST)
– Microsoft.Build (project/solution evaluation)
– Mono.Cecil (assembly metadata / reflection)
– NancyFx (HTTP + routing + DI)
– Newtonsoft.Json (JSON serialization)
– Swagger / Nancy.Swagger (API docs)
E. Test Harness
– OmniSharp.Tests (unit/integration tests organized by feature)
– FakeFileSystem, FakeSolution, etc.
Relationships & data-flow
1. Editor plugin → HTTP POST/GET → NancyFx endpoint
2. Nancy Module (defines route) → invokes corresponding Handler
3. Handler: deserialize Request DTO → invoke one or more Service components
4. Services consult Parser / Solution / Project / FileSystem / MSBuild as needed
5. Service returns Response DTO → NancyFx serializes to JSON → HTTP response back to editor
6. Metadata modules supply Swagger JSON via separate endpoints
Architectural patterns & principles
• Modular “feature per folder” structure (each feature = module + handler + DTOs + metadata)
• Request–response DTOs decoupled from domain models
• Dependency injection via Nancy’s built-in container
• Separation of concerns: API layer vs. orchestration (handlers) vs. core logic vs. parsing/persistence
• Use of adapter/abstraction layers over file system, MSBuild, and parsing