xaizek / zograscope (License: AGPLv3 only) (since 2018-12-07)
Mainly a syntax-aware diff that also provides a number of additional tools.
<root> / tools / gdiff / ZSDiff.hpp (71b26a7fb2fc103d4e5ae1dfceed80a91808b9fb) (3,574B) (mode 100644) [raw]
// Copyright (C) 2018 xaizek <xaizek@posteo.net>
//
// This file is part of zograscope.
//
// zograscope is free software: you can redistribute it and/or modify
// it under the terms of version 3 of the GNU Affero General Public License as
// published by the Free Software Foundation.
//
// zograscope is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with zograscope.  If not, see <http://www.gnu.org/licenses/>.

#ifndef ZOGRASCOPE_TOOLS_GDIFF_ZSDIFF_HPP_
#define ZOGRASCOPE_TOOLS_GDIFF_ZSDIFF_HPP_

#include <map>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>

#include <QMainWindow>

#include "pmr/monolithic.hpp"
#include "tree.hpp"

#include "BlankLineAttr.hpp"
#include "CodeView.hpp"
#include "DiffList.hpp"
#include "FoldTextAttr.hpp"
#include "GuiColorScheme.hpp"

namespace Ui {
class ZSDiff;
}

class QPlainTextEdit;

class Environment;
class Node;
class TimeReport;
class SynHi;

struct TokenInfo
{
    StablePos oldFrom = { 0, 0 };
    StablePos oldTo = { 0, 0 };
    StablePos newFrom = { 0, 0 };
    StablePos newTo = { 0, 0 };
};

enum class LaunchMode
{
    Standalone,
    GitExt,
    Staged,
    Unstaged,
    Commit,
};

class ZSDiff : public QMainWindow
{
    Q_OBJECT

    struct SideInfo;
    struct CodeBuffer;

public:
    ZSDiff(LaunchMode launchMode, DiffList diffList, Environment &env,
           QWidget *parent = nullptr);
    ~ZSDiff();

private:
    void loadDiff(const DiffEntry &diffEntry);
    void updateTitle();
    SideInfo printTree(Tree &tree, CodeView *textEdit, bool original);
    void diffAndPrint(TimeReport &tr);
    void highlightMatch(QPlainTextEdit *textEdit);
    void syncOtherCursor(QPlainTextEdit *textEdit);
    void resolveRange(QPlainTextEdit *textEdit,
                      StablePos fromPos, StablePos toPos,
                      int &from, int &to);
    TokenInfo * getTokenInfo(QPlainTextEdit *textEdit);
    void fold();
    void unfold();
    void updateView(CodeView *view);
    void updateLayout();

    void switchView();

    virtual bool eventFilter(QObject *obj, QEvent *event) override;

    void doSyncMatches();
    // Aligns cursor in the other pane to make it match position of a token
    // that corresponds to one under the cursor of the current pane.
    void alignViews();
    void split(Qt::Orientation orientation);
    void syncScrollTo(CodeView *textEdit);
    bool onlyMode() const;
    CodeView * activeView();
    CodeView * inactiveView();
    CodeView * otherView(CodeView *view);

private:
    Ui::ZSDiff *ui;
    std::unordered_map<const Node *, TokenInfo> info;
    // Whether diff entry is already loaded.
    bool loaded;
    int scrollDiff;
    bool syncScrolls;
    bool syncMatches;
    bool firstTimeFocus;
    bool folded;
    cpp17::pmr::monolithic mr;
    Tree oldTree;
    Tree newTree;
    std::unique_ptr<SynHi> oldSynHi;
    std::unique_ptr<SynHi> newSynHi;
    GuiColorScheme cs;
    std::vector<std::map<int, TokenInfo *>> oldMap, newMap;
    QList<int> splitterSizes;
    BlankLineAttr blankLineAttr;
    FoldTextAttr foldTextAttr;
    // Whether respective lines supposed to be folded.
    std::vector<bool> leftFolded, rightFolded;
    Environment &env;
    LaunchMode launchMode;
    DiffList diffList;
};

#endif // ZOGRASCOPE_TOOLS_GDIFF_ZSDIFF_HPP_
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/zograscope

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/zograscope

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master