All Downloads are FREE. Search and download functionalities are using the official Maven repository.

es.iti.wakamiti.lsp.internal.GherkinWorkspace Maven / Gradle / Ivy

/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

package es.iti.wakamiti.lsp.internal;

import static java.util.stream.Collectors.*;

import java.util.*;
import java.util.stream.*;

import es.iti.wakamiti.api.imconfig.Configuration;
import es.iti.wakamiti.api.util.Pair;
import org.eclipse.lsp4j.*;
import org.yaml.snakeyaml.Yaml;



public class GherkinWorkspace {


    final Map documentAssessors = new HashMap<>();
    final int baseIndex;
    final Yaml yaml = new Yaml();
    final Map> linkMap = new HashMap<>();
    final WorkspaceDiagnosticHelper diagnosticHelper;

    private String configurationUri;
    private TextDocument configurationDocument;

    public GherkinWorkspace(int baseIndex) {
        this.baseIndex = baseIndex;
        this.diagnosticHelper = new WorkspaceDiagnosticHelper(this);
    }

    public Stream addGherkin(String uri, String content) {
        documentAssessors.computeIfAbsent(uri, x-> new GherkinDocumentAssessor(uri,content));
        return computeAllDiagnostics();


    }


    public Stream addConfiguration(String uri, String content) {
        this.configurationUri = uri;
        this.configurationDocument = new TextDocument(content);
        return computeWorkspaceDiagnostics();
    }


    public void addGherkinWithoutDiagnostics(String uri, String content) {
        documentAssessors.computeIfAbsent(uri, x-> new GherkinDocumentAssessor(uri,content));
    }


    public void addConfigurationWithoutDiagnostics(String uri, String content) {
        this.configurationUri = uri;
        this.configurationDocument = new TextDocument(content);
    }




    public Stream updateConfiguration(TextRange range, String text) {
        this.configurationDocument.replaceRange(range, text);
        return computeWorkspaceDiagnostics();
    }

    public DocumentDiagnostics computeDiagnostics(String uri) {
        return document(uri).collectDiagnostics();
    }


    public Stream computeWorkspaceDiagnostics() {
        try {
            var workspaceConfiguration = Configuration.factory().fromMap(
                yaml.load(configurationDocument.rawText())
            );
            documentAssessors.values().forEach(
                document->document.setWorkspaceConfiguration(workspaceConfiguration)
            );
            return computeAllDiagnostics();

        } catch (RuntimeException e) {
            return Stream.empty();
        }
    }



    public Stream update(String uri, TextRange range, String text) {
        if (uri.equals(configurationUri)) {
            return updateConfiguration(range, text);
        } else {
            document(uri).updateDocument(range, text);
            return computeAllDiagnostics();
        }
    }


    public List obtainCodeActions(String uri, List diagnostics) {
        var document = document(uri);

        Stream codeActionsFromDocument = diagnostics.stream()
            .map(document::retrieveQuickFixes)
            .flatMap(List::stream);

        Stream codeActionsFromWorkspace = diagnosticHelper
            .retrieveCodeActions(uri, diagnostics);

        return Stream
            .concat(codeActionsFromDocument, codeActionsFromWorkspace)
            .collect(Collectors.toList());
    }



    public List computeCompletions(String uri, Position position) {
        return document(uri).collectCompletions(
            position.getLine()- baseIndex,
            position.getCharacter() - baseIndex
        );
    }



    public List documentSymbols(String uri) {
        return document(uri).collectSymbols();
    }



    private Stream computeAllDiagnostics() {
        var documentDiagnostics = documentAssessors.values().stream()
            .map(GherkinDocumentAssessor::collectDiagnostics)
            .collect(toList());
        return diagnosticHelper.computeInterDocumentDiagnostics(documentDiagnostics);
    }










    public Optional resolveImplementationLink(String uri, Position position) {
        return Optional.of(document(uri))
            .filter(GherkinDocumentAssessor::isDefinition)
            .flatMap(document -> document.obtainIdAt(position))
            .map(TextSegment::content)
            .map(linkMap::get)
            .map(Pair::value);
    }


    public Optional resolveDefinitionLink(String uri, Position position) {
        return Optional.of(document(uri))
            .filter(GherkinDocumentAssessor::isImplementation)
            .flatMap(document -> document.obtainIdAt(position))
            .map(TextSegment::content)
            .map(linkMap::get)
            .map(Pair::key);
    }


    public Pair format(String uri, int tabSize) {
        var document = document(uri);
        int numberOfLines = document.documentMap.document().numberOfLines();
        int lastPosition = document.documentMap.document().extractLine(numberOfLines-1).length();
        Range range = new Range(new Position(0,0), new Position(numberOfLines, lastPosition));
        String formatted = GherkinFormatter.format(document.documentMap, tabSize);
        return new Pair<>(range,formatted);
    }



    GherkinDocumentAssessor document(String uri) {
        return documentAssessors.computeIfAbsent(uri, x-> new GherkinDocumentAssessor(uri,""));
    }










}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy