-
Notifications
You must be signed in to change notification settings - Fork 54
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 resource to be opened to evaluate method to provide info for tester #400
Conversation
try { | ||
final var context = new EvaluationContext(parent.get(), new Object()); | ||
context.setAllowPluginActivation(true); | ||
if (openResource != null) { | ||
context.addVariable("openResource", openResource); //$NON-NLS-1$ |
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.
LSP4E manipulates more than resources. The common item is the IDocument. Would it be possible to use it instead?
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.
The idea is, that the object behind the openResource variable could be any object like IFile
, IDocument
or IEditorInput
and that the enabledWhen
tester for contentTypeMapping
in the org.eclipse.lsp4e.languageServer
extension point should handle this. In my case a PropertyTester checks the object type and handles it appropriately (similar to HasLanguageServerPropertyTester.java)
I named the variable openResource because it should be the object that LPS4E wants to manipulate. Then I can check in my property tester if LSP4E should do that or not. (files in old C-Projects should not cause clangd language server to be started). Trials with editorInput or viewer variables in my test failed because i have to check the file to be opened not the actual.
Maybe the variable name could be better like openObject or object or manipulatedObject ...
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.
LSP4E manipulates more than resources. The common item is the IDocument. Would it be possible to use it instead?
Using IDocument now in evaluation contest as default variable now
7bde942
to
93c8767
Compare
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.
One important thing is that sometimes, document may not pre-exist. In such case, the operation needs to create the document and then dispose it later. See for example how LSSearchQuery achieves that.
public boolean isEnabled() { | ||
return isUserEnabled() && isExtensionEnabled(); | ||
return isEnabled(null); | ||
} |
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.
Shouldn't we delete those?
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.
We could, then the caller has to use isEnabled(null) when there is no document available (e.g. in org.eclipse.lsp4e.ui.EnableDisableLSJob.run(IProgressMonitor)
:
@Override
protected IStatus run(IProgressMonitor monitor) {
for (ContentTypeToLanguageServerDefinition changedDefinition : serverDefinitions) {
LanguageServerDefinition serverDefinition = changedDefinition.getValue();
if (serverDefinition != null) {
if (!changedDefinition.isEnabled(null)) {
...
I would prefer to delete those methods without argument, because then the developer is urged to provide a IDocument when using this method.
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.
One important thing is that sometimes, document may not pre-exist. In such case, the operation needs to create the document and then dispose it later. See for example how LSSearchQuery achieves that.
I'll check that
public boolean isExtensionEnabled() { | ||
return enablement != null ? enablement.evaluate() : true; | ||
return isExtensionEnabled(null); | ||
} |
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.
Shouldn't we delete those?
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.
same as above
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.
I would prefer to delete those methods without argument, because then the developer is urged to provide a IDocument when using this method.
OK.
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.
@mickaelistria: Updated change. The evaluate method gets an URI and creates a document if its not pre-existing. It will be disposed after the check. Removed isExtensionEnabled()
and isEnabled()
call without arguments. Had to modify the tests according to the removal.
224a090
to
6aeb52b
Compare
Using the URI is a good approach. However I think we should avoid setting a default variable; and instead explicitly include it in the context. I am not certain that the URI is the right variable to make default and thus not setting the default variable allows to not make a mistake that will be hard to resolve. |
For me it's okay to do not use the default variable and use additionalVariables instead. In this case the user has to explicitly use the I would like to add the following variables: |
public boolean evaluate(URI uri) {
boolean temporaryLoadDocument = false;
IResource resource = null;
try {
IDocument document = null;
resource = LSPEclipseUtils.findResourceFor(uri);
if (resource != null) {
document = LSPEclipseUtils.getExistingDocument(resource);
if (document == null) {
document = LSPEclipseUtils.getDocument(resource);
temporaryLoadDocument = true;
}
}
if (document == null) {
temporaryLoadDocument = false;
}
final var context = new EvaluationContext(parent.get(), new Object());
context.addVariable("document", //$NON-NLS-1$
document != null ? document : IEvaluationContext.UNDEFINED_VARIABLE);
context.addVariable("resource", //$NON-NLS-1$
resource != null ? resource : IEvaluationContext.UNDEFINED_VARIABLE);
context.addVariable("uri", //$NON-NLS-1$
uri != null ? uri : IEvaluationContext.UNDEFINED_VARIABLE);
context.setAllowPluginActivation(true);
return expression.evaluate(context).equals(EvaluationResult.TRUE);
} catch (CoreException e) {
LanguageServerPlugin.logError("Error occured during evaluation of enablement expression", e); //$NON-NLS-1$
} finally {
if (temporaryLoadDocument && resource != null) {
try {
FileBuffers.getTextFileBufferManager().disconnect(resource.getFullPath(), LocationKind.IFILE, new NullProgressMonitor());
} catch (CoreException e) {
LanguageServerPlugin.logError(e);
}
}
} |
7e2df8f
to
b498bb9
Compare
b498bb9
to
c23e5f4
Compare
Thanks for this useful addition! |
fix for #393