Sam McCall 25c6257ba0 [clangd] Revamp textDocument/onTypeFormatting.
Summary:
The existing implementation (which triggers on }) is fairly simple and
has flaws:
 - doesn't trigger frequently/regularly enough (particularly in editors that type the }
 for you)
 - often reformats too much code around the edit
 - has jarring cases that I don't have clear ideas for fixing

This implementation is designed to trigger on newline, which feels to me more
intuitive than } or ;.
It does have allow for reformatting after other characters - it has a
basic behavior and a model for adding specialized behavior for
particular characters. But at least initially I'd stick to advertising
\n in the capabilities.

This also handles comment splitting: when you insert a line break inside
a line comment, it will make the new line into an aligned line comment.

Working on tests, but want people to patch it in and try it - it's hard to
see if "feel" is right purely by looking at a test.

Reviewers: ilya-biryukov, hokein

Subscribers: mgorny, ioeric, MaskRay, jkorous, arphaman, kadircet, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D60605

llvm-svn: 362939
2019-06-10 14:26:21 +00:00

57 lines
2.0 KiB
C++

//===--- Format.h - automatic code formatting ---------------*- C++-*------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Clangd uses clang-format for formatting operations.
// This file adapts it to support new scenarios like format-on-type.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_FORMAT_H
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_FORMAT_H
#include "Protocol.h"
#include "clang/Format/Format.h"
#include "clang/Tooling/Core/Replacement.h"
#include "llvm/ADT/StringRef.h"
namespace clang {
namespace clangd {
/// Applies limited formatting around new \p InsertedText.
/// The \p Code already contains the updated text before \p Cursor, and may have
/// had additional / characters (such as indentation) inserted by the editor.
///
/// Example breaking a line (^ is the cursor):
/// === before newline is typed ===
/// if(1){^}
/// === after newline is typed and editor indents ===
/// if(1){
/// ^}
/// === after formatIncremental(InsertedText="\n") ===
/// if (1) {
/// ^
/// }
///
/// We return sorted vector<tooling::Replacement>, not tooling::Replacements!
/// We may insert text both before and after the cursor. tooling::Replacements
/// would merge these, and thus lose information about cursor position.
std::vector<tooling::Replacement>
formatIncremental(llvm::StringRef Code, unsigned Cursor,
llvm::StringRef InsertedText, format::FormatStyle Style);
/// Determine the new cursor position after applying \p Replacements.
/// Analogue of tooling::Replacements::getShiftedCodePosition().
unsigned
transformCursorPosition(unsigned Offset,
const std::vector<tooling::Replacement> &Replacements);
} // namespace clangd
} // namespace clang
#endif