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

com.github.cameltooling.lsp.internal.modelinemodel.CamelKModelineDependencyOption Maven / Gradle / Ivy

There is a newer version: 1.27.0
Show newest version
/**
 * 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
 *
 *      https://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 com.github.cameltooling.lsp.internal.modelinemodel;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Function;
import java.util.stream.Collectors;

import org.apache.camel.catalog.CamelCatalog;
import org.eclipse.lsp4j.CompletionItem;
import org.eclipse.lsp4j.Hover;
import org.eclipse.lsp4j.InsertTextFormat;

import com.github.cameltooling.lsp.internal.catalog.model.ComponentModel;
import com.github.cameltooling.lsp.internal.catalog.util.ModelHelper;
import com.github.cameltooling.lsp.internal.completion.CompletionResolverUtils;
import com.github.cameltooling.lsp.internal.completion.FilterPredicateUtils;

public class CamelKModelineDependencyOption implements ICamelKModelineOptionValue {

	private String value;
	private int startPosition;
	private int line;

	public CamelKModelineDependencyOption(String value, int startPosition, int line) {
		this.value = value;
		this.startPosition = startPosition;
		this.line = line;
	}

	@Override
	public int getStartPositionInLine() {
		return startPosition;
	}

	@Override
	public int getEndPositionInLine() {
		return startPosition + (value != null ? value.length() : 0);
	}

	@Override
	public String getValueAsString() {
		return value;
	}

	@Override
	public CompletableFuture> getCompletions(int position, CompletableFuture camelCatalog) {
		if(getStartPositionInLine() <= position && position <= getEndPositionInLine()) {
			String filter = value != null ? value.substring(0, position - getStartPositionInLine()) : "";
			return camelCatalog
					.thenApply(retrieveCamelComponentCompletionItems(filter))
					.thenApply(addMvnDependencyCompletionItem(filter))
					.thenApply(addJitpackCompletionItem(filter));
		}
		return ICamelKModelineOptionValue.super.getCompletions(position, camelCatalog);
	}

	private Function, List> addJitpackCompletionItem(String filter) {
		return completionItems -> {
			if(filter != null || !"".equals(filter)) {
				completionItems.add(createJitpackCompletionItem());
			}
			return completionItems;
		};
	}

	private CompletionItem createJitpackCompletionItem() {
		CompletionItem completionItem = new CompletionItem("jitpack:.::");
		completionItem.setSortText("1"); // allows to be before Camel Components in completion list
		completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
		completionItem.setInsertText("jitpack:${1|com.github,com.gitlab,com.bitbucket,com.gitee,com.azure|}.${2:username}:${3:repo}:${4:main-SNAPSHOT}");
		CompletionResolverUtils.applyTextEditToCompletionItem(this, completionItem);
		return completionItem;
	}

	private Function, List> addMvnDependencyCompletionItem(String filter) {
		return completionItems -> {
			if(filter != null || !"".equals(filter)) {
				completionItems.add(createMvnCompletionItem());
			}
			return completionItems;
		};
	}

	private Function> retrieveCamelComponentCompletionItems(String filter) {
		return catalog -> catalog.findComponentNames().stream()
			.map(componentName -> ModelHelper.generateComponentModel(catalog.componentJSonSchema(componentName), true))
			.filter(componentModel -> componentModel.getArtifactId() != null)
			.map(componentModel -> {
				CompletionItem completionItem = new CompletionItem(componentModel.getArtifactId());
				completionItem.setDocumentation(componentModel.getDescription());
				CompletionResolverUtils.applyDeprecation(completionItem, componentModel.getDeprecated());
				CompletionResolverUtils.applyTextEditToCompletionItem(this, completionItem);
				return completionItem;
			})
			.filter(FilterPredicateUtils.matchesCompletionFilter(filter))
			.collect(Collectors.toList());
	}

	private CompletionItem createMvnCompletionItem() {
		CompletionItem completionItem = new CompletionItem("mvn:::");
		completionItem.setSortText("1"); // allows to be before Camel Components in completion list
		completionItem.setInsertTextFormat(InsertTextFormat.Snippet);
		completionItem.setInsertText("mvn:${1:groupId}:${2:artifactId}:${3:version}");
		CompletionResolverUtils.applyTextEditToCompletionItem(this, completionItem);
		return completionItem;
	}
	
	@Override
	public CompletableFuture getHover(int characterPosition, CompletableFuture camelCatalog) {
		return camelCatalog.thenApply(catalog -> {
			Optional model = findComponentModel(catalog);
			if (model.isPresent()) {
				return createHover(model.get().getDescription());
			} else {
				return null;
			}
		});
	}

	private Optional findComponentModel(CamelCatalog catalog) {
		return catalog.findComponentNames().stream().map(
				componentName -> ModelHelper.generateComponentModel(catalog.componentJSonSchema(componentName), true))
				.filter(componentModel -> value.equals(componentModel.getArtifactId())).findAny();
	}

	@Override
	public int getLine() {
		return line;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy