blob: 4472ce944d284b58fafe4f9b567e01b715427b2b [file] [log] [blame]
/**
* 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 org.eclipse.lsp4e.test.completion;
import static org.junit.Assert.assertEquals;
import java.util.Collections;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.lsp4e.operations.completion.LSCompletionProposal;
import org.eclipse.lsp4e.test.TestUtils;
import org.eclipse.lsp4e.tests.mock.MockLanguageServer;
import org.eclipse.lsp4j.CompletionItemKind;
import org.eclipse.lsp4j.CompletionList;
import org.eclipse.lsp4j.Position;
import org.eclipse.lsp4j.Range;
import org.eclipse.swt.graphics.Point;
import org.junit.Test;
public class InsertReplaceCompletionTest extends AbstractCompletionTest {
@Test
public void testInsert() throws Exception {
IFile file = TestUtils.createUniqueTestFile(project, "line1\nlineInsertHere");
ITextViewer viewer = TestUtils.openTextViewer(file);
MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(false, Collections.singletonList(
createCompletionItemWithInsertReplace(
"Inserted",
CompletionItemKind.Text,
new Range(new Position(1, 4), new Position(1, 4 + "InsertHere".length())),
new Range(new Position(0,0),new Position(0,0)))
)));
int invokeOffset = viewer.getDocument().getLength() - "InsertHere".length();
ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer, invokeOffset);
LSCompletionProposal lsCompletionProposal = (LSCompletionProposal)proposals[0];
lsCompletionProposal.apply(viewer, '\n', 0, invokeOffset);
assertEquals("line1\nlineInserted", viewer.getDocument().get());
assertEquals(new Point(viewer.getDocument().getLength(), 0), lsCompletionProposal.getSelection(viewer.getDocument()));
}
}