org.eclipse.lsp4j.CreateFileOptions Java Examples

The following examples show how to use org.eclipse.lsp4j.CreateFileOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CodeActionFactory.java    From lemminx with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Makes a CodeAction to create a file and add content to the file.
 * 
 * @param title      The displayed name of the CodeAction
 * @param docURI     The file to create
 * @param content    The text to put into the newly created document.
 * @param diagnostic The diagnostic that this CodeAction will fix
 */
public static CodeAction createFile(String title, String docURI, String content, Diagnostic diagnostic) {

	List<Either<TextDocumentEdit, ResourceOperation>> actionsToTake = new ArrayList<>(2);

	// 1. create an empty file
	actionsToTake.add(Either.forRight(new CreateFile(docURI, new CreateFileOptions(false, true))));

	// 2. update the created file with the given content
	VersionedTextDocumentIdentifier identifier = new VersionedTextDocumentIdentifier(docURI, 0);
	TextEdit te = new TextEdit(new Range(new Position(0, 0), new Position(0, 0)), content);
	actionsToTake.add(Either.forLeft(new TextDocumentEdit(identifier, Collections.singletonList(te))));

	WorkspaceEdit createAndAddContentEdit = new WorkspaceEdit(actionsToTake);

	CodeAction codeAction = new CodeAction(title);
	codeAction.setEdit(createAndAddContentEdit);
	codeAction.setDiagnostics(Collections.singletonList(diagnostic));
	codeAction.setKind(CodeActionKind.QuickFix);

	return codeAction;
}
 
Example #2
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 5 votes vote down vote up
private static void convertCreateCompilationUnitChange(WorkspaceEdit edit, CreateCompilationUnitChange change) {
	ICompilationUnit unit = change.getCu();
	CreateFile createFile = new CreateFile();
	createFile.setUri(JDTUtils.toURI(unit));
	createFile.setOptions(new CreateFileOptions(false, true));
	edit.getDocumentChanges().add(Either.forRight(createFile));
	InsertEdit textEdit = new InsertEdit(0, change.getPreview());
	convertTextEdit(edit, unit, textEdit);
}
 
Example #3
Source File: XMLAssert.java    From lemminx with Eclipse Public License 2.0 4 votes vote down vote up
public static Either<TextDocumentEdit, ResourceOperation> createFile(String uri, boolean overwrite) {
	CreateFileOptions options = new CreateFileOptions();
	options.setIgnoreIfExists(!overwrite);
	options.setOverwrite(overwrite);
	return Either.forRight(new CreateFile(uri, options));
}
 
Example #4
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static void convertRenamePackcageChange(WorkspaceEdit edit, RenamePackageChange packageChange) throws CoreException {
	IPackageFragment pack = (IPackageFragment) packageChange.getModifiedElement();
	IPath newPackageFragment = new Path(packageChange.getNewName().replace('.', IPath.SEPARATOR));
	IPath oldPackageFragment = new Path(packageChange.getOldName().replace('.', IPath.SEPARATOR));
	IPath newPackagePath = pack.getResource().getLocation().removeLastSegments(oldPackageFragment.segmentCount()).append(newPackageFragment);
	if (packageChange.getRenameSubpackages()) {
		IPackageFragment[] allPackages = JavaElementUtil.getPackageAndSubpackages(pack);
		String oldPrefix = packageChange.getOldName();
		for (IPackageFragment currentPackage : allPackages) {
			String newPkgName = packageChange.getNewName() + currentPackage.getElementName().substring(oldPrefix.length());
			//update package's declaration
			convertPackageUpdateEdit(currentPackage.getCompilationUnits(), newPkgName, edit);
		}

		RenameFile renameFile = new RenameFile();
		renameFile.setNewUri(ResourceUtils.fixURI(newPackagePath.toFile().toURI()));
		renameFile.setOldUri(ResourceUtils.fixURI(pack.getResource().getRawLocationURI()));
		edit.getDocumentChanges().add(Either.forRight(renameFile));
	} else {
		//update package's declaration
		convertPackageUpdateEdit(pack.getCompilationUnits(), packageChange.getNewName(), edit);
	
		CreateFile createFile = new CreateFile();
		createFile.setUri(ResourceUtils.fixURI(newPackagePath.append(TEMP_FILE_NAME).toFile().toURI()));
		createFile.setOptions(new CreateFileOptions(false, true));
		edit.getDocumentChanges().add(Either.forRight(createFile));

		for (ICompilationUnit unit : pack.getCompilationUnits()) {
			RenameFile cuResourceChange = new RenameFile();
			cuResourceChange.setOldUri(ResourceUtils.fixURI(unit.getResource().getLocationURI()));
			IPath newCUPath = newPackagePath.append(unit.getPath().lastSegment());
			cuResourceChange.setNewUri(ResourceUtils.fixURI(newCUPath.toFile().toURI()));
			edit.getDocumentChanges().add(Either.forRight(cuResourceChange));
		}

		// Workaround: https://github.com/Microsoft/language-server-protocol/issues/272
		DeleteFile deleteFile = new DeleteFile();
		deleteFile.setUri(ResourceUtils.fixURI(newPackagePath.append(TEMP_FILE_NAME).toFile().toURI()));
		deleteFile.setOptions(new DeleteFileOptions(false, true));
		edit.getDocumentChanges().add(Either.forRight(deleteFile));

	}
}
 
Example #5
Source File: ChangeUtil.java    From eclipse.jdt.ls with Eclipse Public License 2.0 4 votes vote down vote up
private static void convertCreateFileChange(WorkspaceEdit edit, CreateFileChange createFileChange) {
	CreateFile createFile = new CreateFile();
	createFile.setUri(ResourceUtils.fixURI(createFileChange.getPath().toFile().toURI()));
	createFile.setOptions(new CreateFileOptions(false, true));
	edit.getDocumentChanges().add(Either.forRight(createFile));
}
 
Example #6
Source File: CreateFile.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
public CreateFile(@NonNull final String uri, final CreateFileOptions options) {
  super(ResourceOperationKind.Create);
  this.uri = Preconditions.<String>checkNotNull(uri, "uri");
  this.options = options;
}
 
Example #7
Source File: CreateFile.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Additional options
 */
@Pure
public CreateFileOptions getOptions() {
  return this.options;
}
 
Example #8
Source File: CreateFile.java    From lsp4j with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Additional options
 */
public void setOptions(final CreateFileOptions options) {
  this.options = options;
}