Skip to content

Commit

Permalink
Merge pull request #5 from simoleone/support-project-in-subdirectory
Browse files Browse the repository at this point in the history
support ruby project in subdirectory
  • Loading branch information
simoleone authored Dec 10, 2023
2 parents 51be9ac + ff77e7e commit 5a9fe3f
Showing 1 changed file with 18 additions and 14 deletions.
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

0 comments on commit 5a9fe3f

Please sign in to comment.