Skip to content

Commit

Permalink
feat: support out-of-project files
Browse files Browse the repository at this point in the history
Co-Authored-By: Alex Butler <[email protected]>
  • Loading branch information
aminya and alexheretic committed Jun 10, 2021
1 parent d3f213f commit 360d4fb
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
25 changes: 22 additions & 3 deletions lib/auto-languageclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ import * as Utils from "./utils"
import { Socket } from "net"
import { LanguageClientConnection } from "./languageclient"
import { ConsoleLogger, FilteredLogger, Logger } from "./logger"
import { LanguageServerProcess, ServerManager, ActiveServer } from "./server-manager.js"
import {
LanguageServerProcess,
ServerManager,
ActiveServer,
normalizePath,
considerAdditionalPath,
} from "./server-manager.js"
import { Disposable, CompositeDisposable, Point, Range, TextEditor } from "atom"
import * as ac from "atom/autocomplete-plus"
import { basename } from "path"
Expand Down Expand Up @@ -391,6 +397,7 @@ export default class AutoLanguageClient {
connection,
capabilities: initializeResponse.capabilities,
disposable: new CompositeDisposable(),
additionalPaths: new Set<string>(),
}
this.postInitialization(newServer)
connection.initialized()
Expand Down Expand Up @@ -481,10 +488,22 @@ export default class AutoLanguageClient {
/** (Optional) Finds the project path. If there is a custom logic for finding projects override this method. */
protected determineProjectPath(textEditor: TextEditor): string | null {
const filePath = textEditor.getPath()
if (filePath == null) {
// TODO can filePath be null
if (filePath === null || filePath === undefined) {
return null
}
return this._serverManager.getNormalizedProjectPaths().find((d) => filePath.startsWith(d)) || null
const projectPath = this._serverManager.getNormalizedProjectPaths().find((d) => filePath.startsWith(d))
if (projectPath !== undefined) {
return projectPath
}

const serverWithClaim = this._serverManager
.getActiveServers()
.find((server) => server.additionalPaths?.has(path.dirname(filePath)))
if (serverWithClaim !== undefined) {
return normalizePath(serverWithClaim.projectPath)
}
return null
}

/**
Expand Down
12 changes: 12 additions & 0 deletions lib/server-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export interface ActiveServer {
process: LanguageServerProcess
connection: ls.LanguageClientConnection
capabilities: ls.ServerCapabilities
/** Out of project directories that this server can also support. */
additionalPaths?: Set<string>
}

interface RestartCounter {
Expand Down Expand Up @@ -343,3 +345,13 @@ export function normalizedProjectPathToWorkspaceFolder(normalizedProjectPath: st
export function normalizePath(projectPath: string): string {
return !projectPath.endsWith(path.sep) ? path.join(projectPath, path.sep) : projectPath
}

/** Considers a path for inclusion in `additionalPaths`. */
export function considerAdditionalPath(
server: ActiveServer & { additionalPaths: Set<string> },
additionalPath: string
): void {
if (!additionalPath.startsWith(server.projectPath)) {
server.additionalPaths.add(additionalPath)
}
}

0 comments on commit 360d4fb

Please sign in to comment.