Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support ruby project in subdirectory #5

Merged
merged 1 commit into from
Dec 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,23 @@ class SorbetServerSupportProvider : LspServerSupportProvider {

@Suppress("UnstableApiUsage")
private class SorbetLspServerDescriptor(
val sorbetConfigProperties: SorbetConfigProperties, val executionContext: RubyGemExecutionContext, project: Project, roots: Array<out VirtualFile>
) : LspServerDescriptor(project, "Sorbet", *roots) {
val sorbetConfigProperties: SorbetConfigProperties,
val executionContext: RubyGemExecutionContext,
project: Project,
roots: Array<out VirtualFile>
) : LspServerDescriptor(
project, "Sorbet", *roots
) {
override fun isSupportedFile(file: VirtualFile) = file.fileType == RubyFileType.RUBY

override val lspGoToDefinitionSupport: Boolean
get() = sorbetConfigProperties.gotoDefinitionEnabled

override fun startServerProcess(): OSProcessHandler {
val processHandler = executionContext.withWorkingDirPath(project.basePath).withGemScriptName(GEM_SCRIPT_NAME)
.withArguments(GEM_SCRIPT_ARGS).toRubyScriptExecutionContext()!!.createProcessHandler()
val processHandler = executionContext.toRubyScriptExecutionContext()!!.createProcessHandler()
if (processHandler !is OSProcessHandler) {
throw RuntimeException("hmm... RubyProcessHandler wasn't an OSProcessHandler.")
}
LOG.warn(processHandler.toString())
return processHandler
}

Expand All @@ -58,16 +61,17 @@ private class SorbetLspServerDescriptor(

fun createGemExecutionContext(sdk: Sdk, project: Project, file: VirtualFile): RubyGemExecutionContext? {
val module = ModuleUtilCore.findModuleForFile(file, project)
if (BundlerUtil.hasGemfile(module)) {
val gemfile = BundlerUtil.getGemfile(module)
val hasMissingGems = gemfile != null && BundlerGemInfrastructure.hasMissingGems(gemfile)
if (!hasMissingGems) {
return RubyGemExecutionContext.tryCreate(null, module, GEM_NAME)
}
} else {
return RubyGemExecutionContext.tryCreate(sdk, module, GEM_NAME)
val gemfile = BundlerUtil.getGemfile(module) ?: return null // bail if no Gemfile
if (BundlerGemInfrastructure.hasMissingGems(gemfile)) { // bail if bundle install needs to be run
return null
}
return null

return RubyGemExecutionContext.tryCreate(null, module, GEM_NAME)
// NB: Use the Gemfile's parent directory as safest option for typical sorbet project structure.
// Using project content root can get tricky since there may (rarely) be multiple to choose from.
?.withWorkingDir(gemfile.parent)
?.withGemScriptName(GEM_SCRIPT_NAME)
?.withArguments(GEM_SCRIPT_ARGS)
}

fun tryCreate(project: Project, file: VirtualFile): SorbetLspServerDescriptor? {
Expand Down