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

WIP: API для работы с контекстами #2929

Open
wants to merge 2 commits into
base: develop
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,35 @@
package com.github._1c_syntax.bsl.languageserver.scope;

import com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol;

import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

public class BaseScope implements IScope {
@Override
public Set<Capability> getCapabilities() {
return Collections.emptySet();
}

@Override
public Stream<MethodSymbol> getMethods() {
return Stream.empty();
}

@Override
public Stream<IScopeOwner> getProperties() {
return Stream.empty();
}

@Override
public Optional<MethodSymbol> getMethod(String name) {
return Optional.empty();
}

@Override
public Optional<IScopeOwner> getProperty(String name) {
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github._1c_syntax.bsl.languageserver.scope;

public enum Capability {
CLIENT,
SERVER,
PUBLIC
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.github._1c_syntax.bsl.languageserver.scope;

import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
import com.github._1c_syntax.bsl.types.MDOType;
import com.github._1c_syntax.mdclasses.mdo.MDCommonModule;

import java.util.Collection;
import java.util.Set;

public class ConfigurationScope extends SimpleScope {

private static final Collection<MDOType> rootProperties = Set.of(
MDOType.EXCHANGE_PLAN,
MDOType.CONSTANT,
MDOType.CATALOG,
MDOType.DOCUMENT,
MDOType.DOCUMENT_JOURNAL,
MDOType.ENUM,
MDOType.REPORT,
MDOType.DATA_PROCESSOR,
MDOType.CHART_OF_CHARACTERISTIC_TYPES,
MDOType.CHART_OF_ACCOUNTS,
MDOType.CHART_OF_CALCULATION_TYPES,
MDOType.INFORMATION_REGISTER,
MDOType.ACCUMULATION_REGISTER,
MDOType.ACCOUNTING_REGISTER,
MDOType.CALCULATION_REGISTER,
MDOType.BUSINESS_PROCESS,
MDOType.TASK,
MDOType.EXTERNAL_DATA_SOURCE);

private final ServerContext serverContext;

public ConfigurationScope(ServerContext serverContext) {
this.serverContext = serverContext;

for (var module : serverContext.getConfiguration().getCommonModules().values()) {
if (module.isGlobal()) {
appendGlobalCommonModule(module);
} else {
appendCommonModule(module);
}
}

for (var type : rootProperties) {
var property = IScopeOwner.create(type);
properties.put(type.getGroupName(), property);
properties.put(type.getGroupNameRu(), property);
}

}

private void appendCommonModule(MDCommonModule module) {
properties.put(module.getName(), IScopeOwner.create(module));
}

private void appendGlobalCommonModule(MDCommonModule module) {
for (var method : Utils.getMethods(module, serverContext)) {
methods.put(method.getName(), method);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.github._1c_syntax.bsl.languageserver.scope;

import com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol;

import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

public interface IScope {
IScope EMPTY = new BaseScope();

Set<Capability> getCapabilities();

Stream<MethodSymbol> getMethods();

Stream<IScopeOwner> getProperties();

Optional<MethodSymbol> getMethod(String name);

Optional<IScopeOwner> getProperty(String name);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.github._1c_syntax.bsl.languageserver.scope;

import lombok.Getter;

public class IScopeOwner {

public static IScopeOwner create(Object owner) {
return new IScopeOwner(owner);
}

private IScopeOwner(Object owner) {
this.owner = owner;
}

@Getter
private final Object owner;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github._1c_syntax.bsl.languageserver.scope;

import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.utils.Lazy;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class ScopeProvider {

private final ScopeResolver resolver;
private final Lazy<ConfigurationScope> configurationScope = new Lazy<>(this::createConfigurationScope);

public IScope getGlobalScope() {
return UnionContext.builder()
.innerScope(getConfigurationScope())
.innerScope(resolver.getPlatformScope())
.build();
}

public IScope getConfigurationScope() {
return configurationScope.getOrCompute();
}

public IScope getScope(DocumentContext document) {
return UnionContext.builder()
.innerScope(resolver.createFilteredScope(getGlobalScope(), resolver.getCapabilities(document)))
.innerScope(resolver.createScope(document))
.build();
}

public IScope getScope(IScopeOwner owner) {
return resolver.createScope(owner);
}

private ConfigurationScope createConfigurationScope(){
return resolver.createConfigurationScope();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package com.github._1c_syntax.bsl.languageserver.scope;

import com.github._1c_syntax.bsl.languageserver.context.DocumentContext;
import com.github._1c_syntax.bsl.languageserver.context.ServerContext;
import com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol;
import com.github._1c_syntax.bsl.mdo.MDObject;
import com.github._1c_syntax.bsl.mdo.ModuleOwner;
import com.github._1c_syntax.bsl.types.MDOType;
import com.github._1c_syntax.mdclasses.mdo.MDCommonModule;
import com.github._1c_syntax.mdclasses.mdo.MDOHasChildren;
import com.github._1c_syntax.mdclasses.mdo.attributes.AbstractMDOAttribute;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.net.URI;
import java.util.Collections;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;

@Component
@RequiredArgsConstructor
public class ScopeResolver {

private final ServerContext serverContext;

public IScope getPlatformScope() {
return IScope.EMPTY;
}

public IScope createScope(IScopeOwner owner) {
if (owner.getOwner() instanceof MDOType) {
return new MDOTypeScope((MDOType) owner.getOwner(), serverContext);
} else if (owner.getOwner() instanceof MDObject) {
return createScope((MDObject) owner.getOwner());
}
return IScope.EMPTY;
}

public IScope createScope(DocumentContext document) {
return IScope.EMPTY;
}

IScope createFilteredScope(IScope scope, Set<Capability> capabilities) {
return scope;
}

ConfigurationScope createConfigurationScope() {
return new ConfigurationScope(serverContext);
}

IScope createScope(MDObject object) {
var builder = UnionContext.builder();

if (object instanceof MDCommonModule) {
builder.innerScope(new CommonModuleScope((MDCommonModule) object, serverContext));
} else if (object instanceof ModuleOwner) {
var uri = Utils.getManagerModuleURI((ModuleOwner) object, serverContext);
if (uri != null) {
builder.innerScope(new ModuleScope(uri, serverContext));
}
}
// if (object instanceof MDOHasChildren) {
// builder.innerScope(new MDOHasChildrenScope((MDOHasChildren) object));
// }

return builder.build();
}

Set<Capability> getCapabilities(DocumentContext document) {
return Collections.emptySet();
}

@RequiredArgsConstructor
static class MDOTypeScope extends BaseScope {
private final MDOType type;
private final ServerContext serverContext;

@Override
public Stream<IScopeOwner> getProperties() {
return serverContext.getConfiguration().getOrderedTopMDObjects().get(type).stream()
.map(IScopeOwner::create);
}

@Override
public Optional<IScopeOwner> getProperty(String name) {
return serverContext.getConfiguration().getOrderedTopMDObjects().get(type).stream()
.filter(abstractMDObjectBase -> abstractMDObjectBase.getName().equalsIgnoreCase(name))
.map(IScopeOwner::create)
.findFirst();
}
}

@RequiredArgsConstructor
static class CommonModuleScope extends BaseScope {

private final MDCommonModule module;
private final ServerContext serverContext;

@Override
public Stream<MethodSymbol> getMethods() {
return Utils.getMethods(module, serverContext).stream();
}

@Override
public Optional<MethodSymbol> getMethod(String name) {
return Utils.getMethod(module, serverContext, name);
}
}

@RequiredArgsConstructor
static class ModuleScope extends BaseScope {
private final URI moduleUri;
private final ServerContext serverContext;

@Override
public Stream<MethodSymbol> getMethods() {
return Utils.getMethods(moduleUri, serverContext).stream();
}

@Override
public Optional<MethodSymbol> getMethod(String name) {
return Utils.getMethod(moduleUri, serverContext, name);
}
}

@RequiredArgsConstructor
static class MDOHasChildrenScope extends BaseScope {
private final MDOHasChildren object;

@Override
public Stream<IScopeOwner> getProperties() {
return object.getChildren().stream()
.filter(AbstractMDOAttribute.class::isInstance)
.map(IScopeOwner::create);
}

@Override
public Optional<IScopeOwner> getProperty(String name) {
return object.getChildren().stream()
.filter(AbstractMDOAttribute.class::isInstance)
.filter(it -> it.getName().equalsIgnoreCase(name))
.map(IScopeOwner::create)
.findFirst();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.github._1c_syntax.bsl.languageserver.scope;

import com.github._1c_syntax.bsl.languageserver.context.symbol.MethodSymbol;
import org.apache.commons.collections4.map.CaseInsensitiveMap;

import java.util.Optional;
import java.util.stream.Stream;

public class SimpleScope extends BaseScope {
protected final CaseInsensitiveMap<String, MethodSymbol> methods = new CaseInsensitiveMap<>();
protected final CaseInsensitiveMap<String, IScopeOwner> properties = new CaseInsensitiveMap<>();

@Override
public Stream<MethodSymbol> getMethods() {
return methods.values().stream();
}

@Override
public Stream<IScopeOwner> getProperties() {
return properties.values().stream();
}

@Override
public Optional<MethodSymbol> getMethod(String name) {
return Optional.of(methods.get(name));
}

@Override
public Optional<IScopeOwner> getProperty(String name) {
return Optional.ofNullable(properties.get(name));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.github._1c_syntax.bsl.languageserver.scope;

public class ThisObjectResolver {

}
Loading