-
Notifications
You must be signed in to change notification settings - Fork 318
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
Add workspace/executeCommand and clearIndexCache command #131
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,13 @@ package scala.meta.languageserver | |
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.io.PrintStream | ||
import java.io.IOException | ||
import java.nio.file.Files | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
import java.nio.file.SimpleFileVisitor | ||
import java.nio.file.FileVisitResult | ||
import java.nio.file.attribute.BasicFileAttributes | ||
import scala.concurrent.duration.FiniteDuration | ||
import scala.meta.languageserver.compiler.CompilerConfig | ||
import scala.meta.languageserver.compiler.Cursor | ||
|
@@ -36,6 +42,8 @@ import langserver.messages.TextDocumentFormattingRequest | |
import langserver.messages.TextDocumentHoverRequest | ||
import langserver.messages.TextDocumentReferencesRequest | ||
import langserver.messages.TextDocumentSignatureHelpRequest | ||
import langserver.messages.WorkspaceExecuteCommandRequest | ||
import langserver.messages.ExecuteCommandOptions | ||
import langserver.types._ | ||
import monix.eval.Task | ||
import monix.execution.Cancelable | ||
|
@@ -135,6 +143,8 @@ class ScalametaLanguageServer( | |
} | ||
} | ||
|
||
private val clearIndexCacheCommand = "clearIndexCache" | ||
|
||
override def initialize( | ||
request: InitializeParams | ||
): Task[InitializeResult] = Task { | ||
|
@@ -158,7 +168,9 @@ class ScalametaLanguageServer( | |
documentHighlightProvider = true, | ||
documentSymbolProvider = true, | ||
documentFormattingProvider = true, | ||
hoverProvider = true | ||
hoverProvider = true, | ||
executeCommandProvider = | ||
ExecuteCommandOptions(WorkspaceCommand.values.map(_.entryName)) | ||
) | ||
InitializeResult(capabilities) | ||
} | ||
|
@@ -289,6 +301,18 @@ class ScalametaLanguageServer( | |
sourceChangeSubscriber.onNext(input) | ||
} | ||
|
||
override def executeCommand(request: WorkspaceExecuteCommandRequest) = Task { | ||
import WorkspaceCommand._ | ||
WorkspaceCommand | ||
.withNameOption(request.params.command) | ||
.map { | ||
case ClearIndexCache => | ||
logger.info("Clearing the index cache") | ||
ScalametaLanguageServer.clearCacheDirectory() | ||
} | ||
.getOrElse(logger.error(s"Unknown command ${request.params.command}")) | ||
} | ||
|
||
override def onChangeTextDocument( | ||
td: VersionedTextDocumentIdentifier, | ||
changes: Seq[TextDocumentContentChangeEvent] | ||
|
@@ -323,6 +347,27 @@ object ScalametaLanguageServer extends LazyLogging { | |
path | ||
} | ||
|
||
def clearCacheDirectory(): Unit = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if somebody knows a better way of recursively deleting a directory with Java NIO, please let me know! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the best way to do it IMO :) It should be verbose! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On a more serious note, this might merit inclusion in |
||
Files.walkFileTree( | ||
cacheDirectory.toNIO, | ||
new SimpleFileVisitor[Path] { | ||
override def visitFile( | ||
file: Path, | ||
attr: BasicFileAttributes | ||
): FileVisitResult = { | ||
Files.delete(file) | ||
FileVisitResult.CONTINUE | ||
} | ||
override def postVisitDirectory( | ||
dir: Path, | ||
exc: IOException | ||
): FileVisitResult = { | ||
Files.delete(dir) | ||
FileVisitResult.CONTINUE | ||
} | ||
} | ||
) | ||
|
||
def compilerConfigStream(cwd: AbsolutePath)( | ||
implicit scheduler: Scheduler | ||
): (Observer.Sync[AbsolutePath], Observable[CompilerConfig]) = { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package scala.meta.languageserver | ||
|
||
import enumeratum.Enum | ||
import enumeratum.EnumEntry | ||
import enumeratum.EnumEntry.Uncapitalised | ||
|
||
sealed trait WorkspaceCommand extends EnumEntry with Uncapitalised | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I went for a |
||
|
||
case object WorkspaceCommand extends Enum[WorkspaceCommand] { | ||
|
||
case object ClearIndexCache extends WorkspaceCommand | ||
|
||
val values = findValues | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ import { | |
LanguageClientOptions, | ||
ServerOptions, | ||
TransportKind, | ||
RevealOutputChannelOn | ||
RevealOutputChannelOn, | ||
ExecuteCommandRequest | ||
} from 'vscode-languageclient'; | ||
import { Requirements } from './requirements'; | ||
import { exec } from 'child_process'; | ||
|
@@ -91,5 +92,12 @@ export async function activate(context: ExtensionContext) { | |
} | ||
}); | ||
|
||
client.onReady().then(() => { | ||
const clearIndexCacheCommand = commands.registerCommand("scalameta.clearIndexCache", async () => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this needed for every new command? I thought codeActions was a way for the server to list the commands it supports. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it is. codeAction is not very well integrated in the client library, as of now. I guess it may depend by the requirement for extensions to statically include commands in the manifest |
||
return client.sendRequest(ExecuteCommandRequest.type, { command: "clearIndexCache" }); | ||
}); | ||
context.subscriptions.push(clearIndexCacheCommand); | ||
}); | ||
|
||
context.subscriptions.push(client.start(), restartServerCommand); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
arguments
isany[]
in the LSP, so I figuredJsValue
is a good type is.