Search in sources :

Example 21 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class TypeBasedYamlSymbolHandler method createSymbol.

protected SymbolInformation createSymbol(TextDocument doc, Node node, YType type) throws BadLocationException {
    DocumentRegion region = NodeUtil.region(doc, node);
    Location location = new Location(doc.getUri(), doc.toRange(region.getStart(), region.getLength()));
    SymbolInformation symbol = new SymbolInformation();
    symbol.setName(region.toString());
    symbol.setKind(symbolKind(type));
    symbol.setLocation(location);
    symbol.setContainerName(containerName(type));
    return symbol;
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) SymbolInformation(org.eclipse.lsp4j.SymbolInformation) Location(org.eclipse.lsp4j.Location)

Example 22 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class GithubRepoContentAssistant method getOwnerOrRepoCompletions.

private List<ICompletionProposal> getOwnerOrRepoCompletions(CompletionFactory f, DocumentRegion ownerAndRepoRegion) {
    try {
        int slash = ownerAndRepoRegion.indexOf('/');
        if (slash >= 0) {
            DocumentRegion owner = ownerAndRepoRegion.subSequence(0, slash);
            return getRepoCompletions(f, owner, ownerAndRepoRegion.subSequence(slash + 1));
        } else {
            Collection<String> owners = github.getOwners();
            DocumentRegion query = ownerAndRepoRegion;
            if (!owners.isEmpty()) {
                List<ICompletionProposal> proposals = new ArrayList<>(owners.size());
                for (String owner : owners) {
                    if (FuzzyMatcher.matchScore(query, owner) != 0.0) {
                        proposals.add(SimpleCompletionFactory.simpleProposal(query, CompletionItemKind.Text, owner + "/", null, null));
                    }
                }
                return proposals;
            } else {
                return ImmutableList.of();
            }
        }
    } catch (Exception e) {
        return ImmutableList.of(f.errorMessage(ownerAndRepoRegion.toString(), ExceptionUtil.getMessageNoAppendedInformation(e)));
    }
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ICompletionProposal(org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal) ArrayList(java.util.ArrayList)

Example 23 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class GithubValueParsers method uri.

public static ValueParser uri(GithubInfoProvider github) {
    return new ValueParser() {

        @Override
        public GithubRepoReference parse(String _str) throws Exception {
            TextDocument doc = new TextDocument(null, LanguageId.PLAINTEXT);
            doc.setText(_str);
            DocumentRegion str = new DocumentRegion(doc);
            GithubRepoReference repo = parseFormat(str);
            if (repo != null) {
                Collection<String> knownRepos;
                try {
                    knownRepos = github.getReposForOwner(repo.owner.toString());
                } catch (Exception e) {
                    // Couldn't read info from github. Ignore this in reconciler context.
                    return repo;
                }
                if (knownRepos == null) {
                    throw unknownEntity("User or Organization not found: '" + repo.owner + "'", repo.owner);
                } else {
                    if (!knownRepos.contains(repo.name.toString())) {
                        throw unknownEntity("Repo not found: '" + repo.name + "'", repo.name);
                    }
                }
            }
            return repo;
        }

        private GithubRepoReference parseFormat(DocumentRegion str) throws Exception {
            String prefix = checkPrefix(str);
            if (prefix != null) {
                DocumentRegion ownerAndName = str.subSequence(prefix.length());
                // Should end with '.git'
                if (ownerAndName.endsWith(".git")) {
                    ownerAndName = ownerAndName.subSequence(0, ownerAndName.length() - 4);
                } else {
                    DocumentRegion highlight = ownerAndName.textAtEnd(1);
                    throw new ValueParseException("GitHub repo uri should end with '.git'", highlight.getStart(), highlight.getEnd());
                }
                int slash = ownerAndName.indexOf('/');
                if (slash >= 0) {
                    return new GithubRepoReference(ownerAndName.subSequence(0, slash), ownerAndName.subSequence(slash + 1));
                } else {
                    throw new ValueParseException("Expecting something of the form '${owner}/${repo}'.", ownerAndName.getStart(), ownerAndName.getEnd());
                }
            }
            return null;
        }

        private String checkPrefix(DocumentRegion str) throws ValueParseException {
            for (String expectedPrefix : GithubRepoContentAssistant.URI_PREFIXES) {
                int lastChar = expectedPrefix.length() - 1;
                if (str.startsWith(expectedPrefix.substring(0, lastChar))) {
                    if (str.charAt(lastChar) == expectedPrefix.charAt(lastChar)) {
                        return expectedPrefix;
                    }
                    throw new ValueParseException("Expecting a '" + expectedPrefix.charAt(lastChar) + "'", lastChar, lastChar + 1);
                }
            }
            return null;
        }
    };
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) ValueParser(org.springframework.ide.vscode.commons.util.ValueParser) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException) ReconcileException(org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException) ValueParseException(org.springframework.ide.vscode.commons.util.ValueParseException)

Example 24 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class RouteContentAssistant method getQueries.

private String[] getQueries(DocumentRegion region) {
    // Example of what this should do
    // region = "foo.bar.com"
    // We have to make a guess which part of this is the host-name and which part is domain-name
    // The split could be either at the very start, or at any one of the '.' chars.
    // So the possible queries are as follows:
    // - "foo.bar.com"
    // - "bar.com"
    // - "com"
    region = region.trimStart();
    DocumentRegion[] pieces = region.split('.');
    String[] queries = new String[pieces.length];
    for (int i = pieces.length - 1; i >= 0; i--) {
        if (i == pieces.length - 1) {
            queries[i] = pieces[i].toString();
        } else {
            queries[i] = pieces[i] + "." + queries[i + 1];
        }
    }
    return queries;
}
Also used : DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) YValueHint(org.springframework.ide.vscode.commons.yaml.schema.YValueHint)

Example 25 with DocumentRegion

use of org.springframework.ide.vscode.commons.util.text.DocumentRegion in project sts4 by spring-projects.

the class CommonLanguageTools method getValueType.

/**
 * Determine the value type for a give propertyName.
 */
public static Type getValueType(FuzzyMap<PropertyInfo> index, TypeUtil typeUtil, String propertyName) {
    try {
        PropertyInfo prop = index.get(propertyName);
        if (prop != null) {
            return TypeParser.parse(prop.getType());
        } else {
            prop = CommonLanguageTools.findLongestValidProperty(index, propertyName);
            if (prop != null) {
                TextDocument doc = new TextDocument(null, LanguageId.PLAINTEXT);
                doc.setText(propertyName);
                PropertyNavigator navigator = new PropertyNavigator(doc, null, typeUtil, new DocumentRegion(doc, 0, doc.getLength()));
                return navigator.navigate(prop.getId().length(), TypeParser.parse(prop.getType()));
            }
        }
    } catch (Exception e) {
        Log.log(e);
    }
    return null;
}
Also used : TextDocument(org.springframework.ide.vscode.commons.util.text.TextDocument) DocumentRegion(org.springframework.ide.vscode.commons.util.text.DocumentRegion) PropertyNavigator(org.springframework.ide.vscode.boot.properties.reconcile.PropertyNavigator) PropertyInfo(org.springframework.ide.vscode.boot.metadata.PropertyInfo)

Aggregations

DocumentRegion (org.springframework.ide.vscode.commons.util.text.DocumentRegion)25 ICompletionProposal (org.springframework.ide.vscode.commons.languageserver.completion.ICompletionProposal)6 PropertyInfo (org.springframework.ide.vscode.boot.metadata.PropertyInfo)5 ArrayList (java.util.ArrayList)4 BadLocationException (org.springframework.ide.vscode.commons.util.BadLocationException)4 ValueParseException (org.springframework.ide.vscode.commons.util.ValueParseException)4 StsValueHint (org.springframework.ide.vscode.boot.metadata.hints.StsValueHint)3 Type (org.springframework.ide.vscode.boot.metadata.types.Type)3 DocumentEdits (org.springframework.ide.vscode.commons.languageserver.completion.DocumentEdits)3 Renderable (org.springframework.ide.vscode.commons.util.Renderable)3 IRegion (org.springframework.ide.vscode.commons.util.text.IRegion)3 ImmutableList (com.google.common.collect.ImmutableList)2 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)2 Location (org.eclipse.lsp4j.Location)2 SymbolInformation (org.eclipse.lsp4j.SymbolInformation)2 CommonLanguageTools.getValueType (org.springframework.ide.vscode.boot.common.CommonLanguageTools.getValueType)2 EnumCaseMode (org.springframework.ide.vscode.boot.metadata.types.TypeUtil.EnumCaseMode)2 PropertyNavigator (org.springframework.ide.vscode.boot.properties.reconcile.PropertyNavigator)2 ReconcileException (org.springframework.ide.vscode.commons.languageserver.reconcile.ReconcileException)2 IDocument (org.springframework.ide.vscode.commons.util.text.IDocument)2