Skip to content

Commit

Permalink
LSP change dependency command added.
Browse files Browse the repository at this point in the history
  • Loading branch information
sdedic committed Dec 14, 2023
1 parent 14b7a12 commit 79622b0
Show file tree
Hide file tree
Showing 11 changed files with 539 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,12 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import org.netbeans.api.actions.Savable;
import org.netbeans.api.lsp.WorkspaceEdit;
import org.netbeans.modules.project.dependency.impl.ProjectModificationResultImpl;
import org.netbeans.modules.project.dependency.impl.WorkspaceEditAdapter;
Expand All @@ -42,6 +45,13 @@ public final class ProjectModificationResult implements ModificationResult {
this.impl = impl;
}

/**
* @return files that should be save in order so that build system can recognize changes.
*/
public Collection<FileObject> getFilesToSave() {
return impl.getFilesToSave();
}

/**
* Describes the details of the workspace edit.
* @return details of the edit
Expand All @@ -62,9 +72,9 @@ public String getResultingSource(FileObject file) throws IOException, IllegalArg
return wrapEdits().getResultingSource(file);
}

private ModificationResult wrapEdits;
private WorkspaceEditAdapter wrapEdits;

ModificationResult wrapEdits() {
WorkspaceEditAdapter wrapEdits() {
if (wrapEdits == null) {
wrapEdits = new WorkspaceEditAdapter(impl);
}
Expand Down Expand Up @@ -95,9 +105,18 @@ public Collection<? extends File> getNewFiles() {

@Override
public void commit() throws IOException {
wrapEdits().commit();
WorkspaceEditAdapter r = wrapEdits();
r.commit();
if (impl.getCustomEdit() != null) {
impl.getCustomEdit().commit();
}
// save the modified files, so project system will pick things up.
// PENDING: make optional, at the discretion of ProjectDependencyModifier.
for (FileObject f : r.getFilesToSave()) {
Savable s = f.getLookup().lookup(Savable.class);
if (s != null) {
s.save();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.netbeans.api.lsp.ResourceOperation;
import org.netbeans.api.lsp.TextDocumentEdit;
import org.netbeans.api.lsp.TextEdit;
Expand All @@ -50,6 +53,7 @@
public class ProjectModificationResultImpl {
private final Project project;

private Set<FileObject> toSave = new LinkedHashSet<>();
private List<ModificationResult> customModifications = new ArrayList<>();
private List<Union2<TextDocumentEdit, ResourceOperation>> edits;
private ModificationResult combinedResult;
Expand Down Expand Up @@ -90,11 +94,16 @@ public void add(ProjectDependencyModifier.Result r) {
if (r.getWorkspaceEdit() == null) {
return;
}
Collection<FileObject> save = r.requiresSave();
boolean saveAll = save == ProjectDependencyModifier.Result.SAVE_ALL;
if (save != null && !saveAll) {
toSave.addAll(save);
}
for (Union2<TextDocumentEdit, ResourceOperation> op : r.getWorkspaceEdit().getDocumentChanges()) {
if (op.hasSecond()) {
addResourceOperation(op.second());
} else if (op.hasFirst()) {
addTextOperation(op.first());
addTextOperation(op.first(), saveAll);
}
}
}
Expand Down Expand Up @@ -190,7 +199,11 @@ public int compare(TextEdit o1, TextEdit o2) {
};
}

private void addTextOperation(TextDocumentEdit edit) {
public Collection<FileObject> getFilesToSave() {
return toSave;
}

private void addTextOperation(TextDocumentEdit edit, boolean saveAll) {
FileObject fo = fromString(edit.getDocument());
if (fo == null) {
throw new ProjectOperationException(project, ProjectOperationException.State.ERROR,
Expand All @@ -203,7 +216,9 @@ private void addTextOperation(TextDocumentEdit edit) {
Bundle.ERR_WritingToMissingFile(edit.getDocument()), Collections.emptySet());
}
}

if (saveAll) {
toSave.add(fo);
}
TextDocumentEdit tde = fileModifications.get(fo);
List<TextEdit> newEdits = new ArrayList<>(edit.getEdits());
Collections.sort(newEdits, textEditComparator(edit.getEdits()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import org.netbeans.api.lsp.ResourceOperation;
Expand All @@ -30,6 +32,7 @@
import org.netbeans.modules.refactoring.spi.ModificationResult;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
import org.openide.filesystems.URLMapper;
import org.openide.util.NbBundle;
import org.openide.util.Union2;

Expand All @@ -44,6 +47,23 @@ public final class WorkspaceEditAdapter implements ModificationResult {
public WorkspaceEditAdapter(ProjectModificationResultImpl impl) {
this.impl = impl;
}

public Collection<FileObject> getFilesToSave() {
List<FileObject> processed = new ArrayList<>();
for (FileObject f : impl.getFilesToSave()) {
if (f.isVirtual()) {
FileObject changed = URLMapper.findFileObject(f.toURL());
if (changed == null) {
continue;
}
f = changed;
}
if (f.isValid()) {
processed.add(f);
}
}
return processed;
}

@Override
public String getResultingSource(FileObject file) throws IOException, IllegalArgumentException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,13 @@
*/
package org.netbeans.modules.project.dependency.spi;

import java.util.Collection;
import java.util.Collections;
import org.netbeans.api.lsp.WorkspaceEdit;
import org.netbeans.modules.project.dependency.DependencyChangeException;
import org.netbeans.modules.project.dependency.DependencyChangeRequest;
import org.netbeans.modules.project.dependency.ProjectOperationException;
import org.openide.filesystems.FileObject;

/**
* Computes dependency modifications to project files. Must be registered in the project's
Expand All @@ -45,6 +48,16 @@ public interface ProjectDependencyModifier {
* Result of dependency modification change.
*/
public interface Result {
public static final Collection<FileObject> SAVE_ALL = Collections.singleton(null);

/**
* Returns list of files that require save.
* @return files to save.
*/
public default Collection<FileObject> requiresSave() {
return SAVE_ALL;
}

/**
* ID of the partial result. Mainly used to override / suppress unwanted changes by
* more specific Modified implementations.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* 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.project.dependency.spi;

import java.io.IOException;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import org.netbeans.api.project.Project;
import org.openide.filesystems.FileObject;

/**
* Provides information on files affecting the project reload, and allows to reload project metadata.
* The project infrastructure usually monitors the on-disk file changes and manages background project reloads.
* But with programmatic changes to project files, it may be necessary to wait for the project reload to pick
* the new project's metadata. The project infrastructure may not be able to pick in-memory document changes
* to the project settings; especially when invokes external tools such as Maven, Gradle etc. This interface
* also allows to collect project files, that should be saved before project reload can pick fresh data.
* @since 1.7
* @author sdedic
*/
public interface ProjectReloadImplementation {
/**
* Attempts to find the set of files. It will return FileObjects representing
* files that contain project's definition. The implementation may also indicate
* that it needs to sync project to disk in order to do project reload. If
* `forProjectLoad` is true, then reported files should be saved before reloading
* the project, otherwise the project metadata can still contain obsolete info. Note
* that the set of files is computed from the current project's metadata, so if the
* unsaved change contains gross changes, such pas parent POM change, the reported set
* of files may not be complete. The report for project load may also contain
* files from other projects.
* <p/>
* Implementations, that can analyze in-memory state may return an empty set for this
* case.
* @param forProjectLoad if true, implementation should report files that must be
* saved before project load could load fresh information
* @return set of project files.
*/
public Set<FileObject> findProjectFiles(boolean forProjectLoad);

/**
* Attempts to reload project metadata, to reflect the current project state. Note that
* the resulting Future may report an {@link IOException} instead of a Project instance in
* the case that the project loading fails.
*
* @return a Future that will be completed when the project reloads.
*/
public CompletableFuture<Project> reloadProject();
}
8 changes: 8 additions & 0 deletions java/java.lsp.server/nbcode/integration/nbproject/project.xml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@
<specification-version>1.104.0.8</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.refactoring.api</code-name-base>
<build-prerequisite/>
<compile-dependency/>
<run-dependency>
<specification-version>1.70.0.1</specification-version>
</run-dependency>
</dependency>
<dependency>
<code-name-base>org.netbeans.modules.updatecenters</code-name-base>
<run-dependency>
Expand Down
Loading

0 comments on commit 79622b0

Please sign in to comment.