Skip to content

Commit

Permalink
Index Builtins Suggestions (#1575)
Browse files Browse the repository at this point in the history
PR enables suggestions indexing for
the Builtins module
  • Loading branch information
4e6 authored Mar 17, 2021
1 parent 21f6673 commit 35a0db1
Show file tree
Hide file tree
Showing 8 changed files with 295 additions and 1,327 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class RuntimeOptions {
OptionDescriptor.newBuilder(LOG_LEVEL_KEY, LOG_LEVEL).build();

public static final String INTERPRETER_SEQUENTIAL_COMMAND_EXECUTION =
interpreterOptionName(".sequentialCommandExecution");
interpreterOptionName("sequentialCommandExecution");
public static final OptionKey<Boolean> INTERPRETER_SEQUENTIAL_COMMAND_EXECUTION_KEY =
new OptionKey<>(false);
public static final OptionDescriptor INTERPRETER_SEQUENTIAL_COMMAND_EXECUTION_DESCRIPTOR =
Expand All @@ -39,14 +39,26 @@ public class RuntimeOptions {
INTERPRETER_SEQUENTIAL_COMMAND_EXECUTION)
.build();

public static final String ENABLE_PROJECT_SUGGESTIONS = optionName("enableProjectSuggestions");
public static final OptionKey<Boolean> ENABLE_PROJECT_SUGGESTIONS_KEY = new OptionKey<>(true);
private static final OptionDescriptor ENABLE_PROJECT_SUGGESTIONS_DESCRIPTOR =
OptionDescriptor.newBuilder(ENABLE_PROJECT_SUGGESTIONS_KEY, ENABLE_PROJECT_SUGGESTIONS).build();

public static final String ENABLE_GLOBAL_SUGGESTIONS = optionName("enableGlobalSuggestions");
public static final OptionKey<Boolean> ENABLE_GLOBAL_SUGGESTIONS_KEY = new OptionKey<>(true);
private static final OptionDescriptor ENABLE_GLOBAL_SUGGESTIONS_DESCRIPTOR =
OptionDescriptor.newBuilder(ENABLE_GLOBAL_SUGGESTIONS_KEY, ENABLE_GLOBAL_SUGGESTIONS).build();

public static final OptionDescriptors OPTION_DESCRIPTORS =
OptionDescriptors.create(
Arrays.asList(
PACKAGES_PATH_DESCRIPTOR,
STRICT_ERRORS_DESCRIPTOR,
LOG_LEVEL_DESCRIPTOR,
DISABLE_INLINE_CACHES_DESCRIPTOR,
INTERPRETER_SEQUENTIAL_COMMAND_EXECUTION_DESCRIPTOR));
INTERPRETER_SEQUENTIAL_COMMAND_EXECUTION_DESCRIPTOR,
ENABLE_PROJECT_SUGGESTIONS_DESCRIPTOR,
ENABLE_GLOBAL_SUGGESTIONS_DESCRIPTOR));

/**
* Canonicalizes the option name by prefixing it with the language name.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,24 @@ public boolean isStrictErrors() {
return getEnvironment().getOptions().get(RuntimeOptions.STRICT_ERRORS_KEY);
}

/**
* Checks whether the suggestions indexing is enabled for project files.
*
* @return true if project-level suggestion indexing is enabled.
*/
public boolean isProjectSuggestionsEnabled() {
return getEnvironment().getOptions().get(RuntimeOptions.ENABLE_PROJECT_SUGGESTIONS_KEY);
}

/**
* Checks whether the suggestion indexing is enabled for external libraries.
*
* @return true if the suggestions indexing is enabled for external libraries.
*/
public boolean isGlobalSuggestionsEnabled() {
return getEnvironment().getOptions().get(RuntimeOptions.ENABLE_GLOBAL_SUGGESTIONS_KEY);
}

/** Creates a new thread that has access to the current language context. */
public Thread createThread(Runnable runnable) {
return environment.createThread(runnable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class EnsureCompiledJob(protected val files: Iterable[File])
/** @inheritdoc */
override def run(implicit ctx: RuntimeContext): CompilationStatus = {
ctx.locking.acquireWriteCompilationLock()

try {
val compilationResult = ensureCompiledFiles(files)
ctx.contextManager.getAll.values.foreach { stack =>
Expand Down Expand Up @@ -73,8 +74,7 @@ class EnsureCompiledJob(protected val files: Iterable[File])
ctx.executionService.getContext.getModuleForFile(file).toScala
}
val modulesInScope =
ctx.executionService.getContext.getTopScope.getModules.asScala
.filterNot(m => modules.exists(_ == m))
getModulesInScope.filterNot(m => modules.exists(_ == m))
val moduleCompilationStatus = modules.map(ensureCompiledModule)
val scopeCompilationStatus = ensureCompiledScope(modulesInScope)
(moduleCompilationStatus ++ scopeCompilationStatus).maxOption
Expand All @@ -97,7 +97,9 @@ class EnsureCompiledJob(protected val files: Iterable[File])
val cacheInvalidationCommands =
buildCacheInvalidationCommands(changeset, module.getLiteralSource)
runInvalidationCommands(cacheInvalidationCommands)
analyzeModule(module, changeset)
if (ctx.executionService.getContext.isProjectSuggestionsEnabled) {
analyzeModule(module, changeset)
}
runCompilationDiagnostics(module)
}
.getOrElse(CompilationStatus.Failure)
Expand Down Expand Up @@ -126,7 +128,9 @@ class EnsureCompiledJob(protected val files: Iterable[File])
)
CompilationStatus.Failure
case Right(module) =>
analyzeModuleInScope(module)
if (ctx.executionService.getContext.isGlobalSuggestionsEnabled) {
analyzeModuleInScope(module)
}
runCompilationDiagnostics(module)
}
}
Expand All @@ -144,11 +148,7 @@ class EnsureCompiledJob(protected val files: Iterable[File])
e
)
}
if (
!module.isIndexed &&
module.getLiteralSource != null &&
module.getPath != null
) {
if (!module.isIndexed && module.getLiteralSource != null) {
ctx.executionService.getLogger
.log(Level.FINEST, s"Analyzing module in scope ${module.getName}")
val moduleName = module.getName
Expand All @@ -157,7 +157,7 @@ class EnsureCompiledJob(protected val files: Iterable[File])
.filter(isSuggestionGlobal)
val version = ctx.versioning.evalVersion(module.getLiteralSource.toString)
val notification = Api.SuggestionsDatabaseModuleUpdateNotification(
file = new File(module.getPath),
file = getIndexingPath(module),
version = version,
actions =
Vector(Api.SuggestionsDatabaseAction.Clean(moduleName.toString)),
Expand Down Expand Up @@ -430,6 +430,26 @@ class EnsureCompiledJob(protected val files: Iterable[File])
case _ => None
}

/** Get all modules in the current compiler scope. */
private def getModulesInScope(implicit
ctx: RuntimeContext
): Iterable[Module] = {
val topScope = ctx.executionService.getContext.getTopScope
val modulesInScope = topScope.getModules.asScala
val builtins = topScope.getBuiltins.getModule
modulesInScope ++ Seq(builtins)
}

/** Get the module path for suggestions database indexing.
*
* If the module is synthetic (i.e. Builtins), uses its name as a path.
*/
private def getIndexingPath(module: Module): File =
if (module.getPath eq null) {
new File(s"/${module.getName}")
} else
new File(module.getPath)

}

object EnsureCompiledJob {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

@scala.annotation.nowarn("msg=multiarg infix syntax")
class ExpressionErrorsTest
class RuntimeErrorsTest
extends AnyFlatSpec
with Matchers
with BeforeAndAfterEach {
Expand Down Expand Up @@ -55,6 +55,8 @@ class ExpressionErrorsTest
.option(RuntimeOptions.PACKAGES_PATH, pkg.root.getAbsolutePath)
.option(RuntimeOptions.LOG_LEVEL, "WARNING")
.option(RuntimeOptions.INTERPRETER_SEQUENTIAL_COMMAND_EXECUTION, "true")
.option(RuntimeOptions.ENABLE_PROJECT_SUGGESTIONS, "false")
.option(RuntimeOptions.ENABLE_GLOBAL_SUGGESTIONS, "false")
.option(RuntimeServerInfo.ENABLE_OPTION, "true")
.out(out)
.serverTransport { (uri, peer) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ class RuntimeInstrumentTest
.option(RuntimeOptions.PACKAGES_PATH, pkg.root.getAbsolutePath)
.option(RuntimeOptions.LOG_LEVEL, "WARNING")
.option(RuntimeOptions.INTERPRETER_SEQUENTIAL_COMMAND_EXECUTION, "true")
.option(RuntimeOptions.ENABLE_PROJECT_SUGGESTIONS, "false")
.option(RuntimeOptions.ENABLE_GLOBAL_SUGGESTIONS, "false")
.option(RuntimeServerInfo.ENABLE_OPTION, "true")
.out(out)
.serverTransport { (uri, peer) =>
Expand Down
Loading

0 comments on commit 35a0db1

Please sign in to comment.