Skip to content

Commit

Permalink
Navigation support for property groups. Corrections to props indexing.
Browse files Browse the repository at this point in the history
  • Loading branch information
BoykoAlex committed Apr 27, 2023
1 parent c7b5cc8 commit e9b7518
Show file tree
Hide file tree
Showing 22 changed files with 1,239 additions and 241 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -785,18 +785,22 @@ public String toString() {
return "Editor(\n"+getText()+"\n)";
}

public void assertLinkTargets(String hoverOver, Set<LocationLink> expectedLocations) throws Exception {
public void assertLinkTargets(String hoverOver, List<LocationLink> expectedLocations) throws Exception {
int pos = getRawText().indexOf(hoverOver);
if (pos>=0) {
pos += hoverOver.length() / 2;
}
assertTrue(pos>=0, "Not found in editor: '"+hoverOver+"'");

DefinitionParams params = new DefinitionParams(new TextDocumentIdentifier(getUri()), doc.toPosition(pos));

assertLinkTargets(doc.toPosition(pos), expectedLocations);
}

public void assertLinkTargets(Position pos, List<LocationLink> expectedLocations) throws Exception {
DefinitionParams params = new DefinitionParams(new TextDocumentIdentifier(getUri()), pos);
List<? extends LocationLink> definitions = harness.getDefinitions(params);

assertEquals(ImmutableSet.copyOf(expectedLocations), ImmutableSet.copyOf(definitions));
assertEquals(ImmutableList.copyOf(expectedLocations), ImmutableList.copyOf(definitions));
}


public void assertNoLinkTargets(String hoverOver) throws Exception {
int pos = getRawText().indexOf(hoverOver);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
* @author Stephane Nicoll
* @since 1.3.0
*/
class ConfigurationMetadataItem extends ConfigurationMetadataProperty {
public class ConfigurationMetadataItem extends ConfigurationMetadataProperty {

private String sourceType;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2014-2016 Pivotal, Inc.
* Copyright (c) 2014, 2023 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -14,13 +14,13 @@
import java.util.Collections;
import java.util.List;

import org.springframework.ide.vscode.boot.configurationmetadata.ConfigurationMetadataItem;
import org.springframework.ide.vscode.boot.configurationmetadata.ConfigurationMetadataProperty;
import org.springframework.ide.vscode.boot.configurationmetadata.ConfigurationMetadataSource;
import org.springframework.ide.vscode.boot.configurationmetadata.Deprecation;
import org.springframework.ide.vscode.boot.configurationmetadata.Deprecation.Level;
import org.springframework.ide.vscode.boot.configurationmetadata.ValueHint;
import org.springframework.ide.vscode.boot.configurationmetadata.ValueProvider;
import org.springframework.ide.vscode.boot.java.links.JavaElementLocationProvider;
import org.springframework.ide.vscode.boot.metadata.ValueProviderRegistry.ValueProviderStrategy;
import org.springframework.ide.vscode.boot.metadata.hints.HintProvider;
import org.springframework.ide.vscode.boot.metadata.hints.HintProviders;
Expand Down Expand Up @@ -54,6 +54,11 @@ public PropertySource(ConfigurationMetadataSource source) {
this.sourceType = st!=null?st:source.getType();
this.sourceMethod = source.getSourceMethod();
}

public PropertySource(String sourceType, String sourceMethod) {
this.sourceType = sourceType;
this.sourceMethod = sourceMethod;
}
@Override
public String toString() {
return sourceType+"::"+sourceMethod;
Expand Down Expand Up @@ -154,6 +159,10 @@ public PropertyInfo(ValueProviderRegistry valueProviders, ConfigurationMetadataP
handleKeyAs(h.getParameters().get("target"));
}
}
if (prop instanceof ConfigurationMetadataItem) {
ConfigurationMetadataItem item = (ConfigurationMetadataItem) prop;
addSource(new PropertySource(item.getSourceType(), item.getSourceMethod()));
}
}
public PropertyInfo(String p) {
this(p, null, null, null, null, null, null, null, null, null, null);
Expand All @@ -179,6 +188,17 @@ public String getType() {
public String getName() {
return name;
}
/**
* Gets the name of the property without the x.y.z prefix, i.e. if property is x.y.z.a then its simple name is a
* @return simple name
*/
public String getSimpleName() {
int idx = name.lastIndexOf('.');
if (idx >= 0 && idx < name.length() - 1) {
return name.substring(idx + 1);
}
return name;
}
public Object getDefaultValue() {
return defaultValue;
}
Expand Down Expand Up @@ -217,14 +237,18 @@ public String toString() {
return "PropertyInfo("+getId()+")";
}
public PropertySource addSource(ConfigurationMetadataSource source) {
if (sources==null) {
sources = new ArrayList<PropertySource>();
}
PropertySource s = new PropertySource(source);
sources.add(s);
addSource(s);
return s;
}

public void addSource(PropertySource source) {
if (sources==null) {
sources = new ArrayList<PropertySource>();
}
sources.add(source);
}

public PropertyInfo withId(String alias) {
if (alias.equals(id)) {
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2015 Pivotal, Inc.
* Copyright (c) 2015, 2023 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -59,14 +59,18 @@ private void addMetadata(ConfigurationMetadataRepository metadata) {
}

for (ConfigurationMetadataGroup group : metadata.getAllGroups().values()) {
ImmutableSet.Builder<PropertySource> sources = ImmutableSet.builder();
for (ConfigurationMetadataSource source : group.getSources().values()) {
ImmutableSet.Builder<PropertySource> sources = ImmutableSet.builder();
PropertySource propertySource = new PropertySource(source);
sources.add(propertySource);
for (ConfigurationMetadataProperty prop : source.getProperties().values()) {
PropertyInfo info = properties.get(prop.getId());
sources.add(info.addSource(source));
if (info.getSources().isEmpty()) {
info.addSource(propertySource);
}
}
groups.put(group.getId(), sources.build());
}
groups.put(group.getId(), sources.build());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,24 @@ private static IMember getPropertySourceJavaElement(TypeUtil typeUtil, IJavaProj
elements.add(type);
String methodSig = source.getSourceMethod();
if (methodSig!=null) {
// the property source is a method, so actually we look for accessor in the return type.
IMethod method = getMethod(type, methodSig);
if (method!=null) {
elements.add(method);
if (type.isRecord()) {
IField field = type.getField(getMethodName(methodSig));
if (field != null) {
// Prefer field over method for records if names are the same
elements.add(field);
} else {
// No field? Add a method if found then.
IMethod method = getMethod(type, methodSig);
if (method!=null) {
elements.add(method);
}
}
} else {
// the property source is a method, so actually we look for accessor in the return type.
IMethod method = getMethod(type, methodSig);
if (method!=null) {
elements.add(method);
}
}
}
}
Expand Down Expand Up @@ -140,12 +154,12 @@ private static IMember getPropertyJavaElement(TypeUtil typeUtil, IJavaProject pr
}
}
if (type != null && type.isRecord()) {
IField field = getPropertyField(type, property.getName());
IField field = getPropertyField(type, property.getSimpleName());
if (field != null) {
elements.add(field);
}
} else {
IMethod method = getPropertyMethod(typeUtil, type, property.getName());
IMethod method = getPropertyMethod(typeUtil, type, property.getSimpleName());
if (method!=null) {
elements.add(method);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2019 Pivotal, Inc.
* Copyright (c) 2019, 2023 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -168,7 +168,7 @@ void testBeanRefHyperlink() throws Exception {
targetRange,
editor.rangeOf("name=\"simple\" ref=\"simpleObj\"", "simpleObj")
);
editor.assertLinkTargets("simpleObj", Collections.singleton(expectedLocation));
editor.assertLinkTargets("simpleObj", Collections.singletonList(expectedLocation));
}

@Test
Expand Down Expand Up @@ -238,7 +238,7 @@ void testBeanRefHyperlink_SpecifyScanFolderDifferently() throws Exception {
targetRange,
editor.rangeOf("name=\"simple\" ref=\"simpleObj\"", "simpleObj")
);
editor.assertLinkTargets("simpleObj", Collections.singleton(expectedLocation));
editor.assertLinkTargets("simpleObj", Collections.singletonList(expectedLocation));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.lsp4j.LocationLink;
Expand Down Expand Up @@ -88,7 +89,7 @@ public class TestValueCompletion {
new Range(new Position(0, 0), new Position(0, 11)), new Range(new Position(0, 10), new Position(0, 11)),
new Range(new Position(6, 8), new Position(6, 22)));

editor.assertLinkTargets("some.prop", Set.of(expectedLocation));
editor.assertLinkTargets("some.prop", List.of(expectedLocation));
}

@Test
Expand All @@ -112,7 +113,7 @@ public class TestValueCompletion {
new Range(new Position(1, 2), new Position(1, 9)), new Range(new Position(1, 8), new Position(1, 9)),
new Range(new Position(6, 8), new Position(6, 22)));

editor.assertLinkTargets("some.prop", Set.of(expectedLocation));
editor.assertLinkTargets("some.prop", List.of(expectedLocation));
}

@Test
Expand Down Expand Up @@ -140,7 +141,7 @@ public class TestValueCompletion {
new Range(new Position(1, 2), new Position(1, 9)), new Range(new Position(1, 8), new Position(1, 9)),
new Range(new Position(6, 8), new Position(6, 22)));

editor.assertLinkTargets("some.prop", Set.of(expectedPropsLocation, expectedYamlLocation));
editor.assertLinkTargets("some.prop", List.of(expectedYamlLocation, expectedPropsLocation));

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2016, 2019 Pivotal, Inc.
* Copyright (c) 2016, 2023 Pivotal, Inc.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand Down Expand Up @@ -40,7 +40,7 @@ void springStandardPropertyPresent_Maven() throws Exception {
PropertyInfo propertyInfo = index.get("server.port");
assertNotNull(propertyInfo);
assertEquals(Integer.class.getName(), propertyInfo.getType());
assertEquals("port", propertyInfo.getName());
assertEquals("port", propertyInfo.getSimpleName());
}

@Test
Expand All @@ -52,7 +52,7 @@ void customPropertyPresent_Maven() throws Exception {
PropertyInfo propertyInfo = index.get("demo.settings.user");
assertNotNull(propertyInfo);
assertEquals(String.class.getName(), propertyInfo.getType());
assertEquals("user", propertyInfo.getName());
assertEquals("user", propertyInfo.getSimpleName());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -488,19 +488,12 @@ void testHyperlinkTargets() throws Exception {

Editor editor = newEditor(
"server.port=888\n" +
"spring.datasource.login-timeout=1000\n" +
"flyway.init-sqls=a,b,c\n"
);

definitionLinkAsserts.assertLinkTargets(editor, "server", p, editor.rangeOf("server.port", "server.port"),
method("org.springframework.boot.autoconfigure.web.ServerProperties", "setPort", "java.lang.Integer"));

definitionLinkAsserts.assertLinkTargets(editor, "data", p, editor.rangeOf("spring.datasource.login-timeout", "spring.datasource.login-timeout"),
method("org.springframework.boot.autoconfigure.jdbc.DataSourceConfigMetadata", "hikariDataSource"),
method("org.springframework.boot.autoconfigure.jdbc.DataSourceConfigMetadata", "tomcatDataSource"),
method("org.springframework.boot.autoconfigure.jdbc.DataSourceConfigMetadata", "dbcpDataSource")
);

definitionLinkAsserts.assertLinkTargets(editor, "flyway", p, editor.rangeOf("flyway.init-sqls", "flyway.init-sqls"),
method("org.springframework.boot.autoconfigure.flyway.FlywayProperties", "setInitSqls", "java.util.List"));
}
Expand Down
Loading

0 comments on commit e9b7518

Please sign in to comment.