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

Run long-spanning tasks cancellably on background in the (Java) LSP server. #7708

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
@@ -0,0 +1,145 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.java.lsp.server;

import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.netbeans.api.java.source.CancellableTask;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.api.java.source.JavaSource.Phase;
import org.netbeans.api.java.source.JavaSource.Priority;
import org.netbeans.api.java.source.JavaSourceTaskFactory;
import org.netbeans.modules.parsing.spi.TaskIndexingMode;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.util.Lookup;
import org.openide.util.Pair;
import org.openide.util.lookup.ServiceProvider;
import org.openide.util.lookup.ServiceProviders;

@ServiceProviders({
@ServiceProvider(service=JavaSourceTaskFactory.class),
@ServiceProvider(service=RunOnceFactory.class)
})
public class RunOnceFactory extends JavaSourceTaskFactory {

private static final Logger LOG = Logger.getLogger(RunOnceFactory.class.getName());

private List<Pair<FileObject, CancellableTask<CompilationInfo>>> work = new LinkedList<>();
private FileObject currentFile;
private CancellableTask<CompilationInfo> task;

public RunOnceFactory() {
super(Phase.RESOLVED, Priority.BELOW_NORMAL, TaskIndexingMode.ALLOWED_DURING_SCAN);
// INSTANCE = this;
}

protected CancellableTask<CompilationInfo> createTask(FileObject file) {
return new CancellableTask<CompilationInfo>() {
private final AtomicReference<CancellableTask<CompilationInfo>> currentTask =
new AtomicReference<>();

public void cancel() {
CancellableTask<CompilationInfo> task = currentTask.get();

if (task != null) {
task.cancel();
}
}
public void run(CompilationInfo parameter) throws Exception {
CancellableTask<CompilationInfo> task;

synchronized (RunOnceFactory.this) {
task = RunOnceFactory.this.task;
}

currentTask.set(task);

task.run(parameter);

currentTask.set(null);
next();
}
};
}

protected synchronized Collection<FileObject> getFileObjects() {
if (currentFile == null)
return Collections.<FileObject>emptyList();

return Collections.<FileObject>singletonList(currentFile);
}

private synchronized void addImpl(FileObject file, CancellableTask<CompilationInfo> task) {
if (LOG.isLoggable(Level.FINE)) {
LOG.log(Level.FINE, "addImpl({0}, {1})", new Object[] {FileUtil.getFileDisplayName(file), task.getClass().getName()});
}

work.add(Pair.<FileObject, CancellableTask<CompilationInfo>>of(file, task));

if (currentFile == null)
next();
}

private synchronized void next() {
LOG.fine("next, phase 1");

if (work.isEmpty()) {
if (currentFile != null) {
LOG.fine("clearing current file");
currentFile = null;
task = null;
fileObjectsChanged();
}

LOG.fine("work is empty");
return ;
}

LOG.fine("next, phase 2");

Pair<FileObject, CancellableTask<CompilationInfo>> p = work.remove(0);

task = p.second();

if (currentFile == p.first()) {
reschedule(currentFile);
} else {
currentFile = p.first();
fileObjectsChanged();
}

LOG.fine("next, phase 2 done");
}


public static void add(FileObject file, CancellableTask<CompilationInfo> task) {
RunOnceFactory factory = Lookup.getDefault().lookup(RunOnceFactory.class);

if (factory == null)
return ;

factory.addImpl(file, task);
}
}
Loading
Loading