This repository was archived by the owner on Dec 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathclient_collaborator.cc
More file actions
149 lines (127 loc) · 4.44 KB
/
client_collaborator.cc
File metadata and controls
149 lines (127 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright 2017 Google LLC
//
// Licensed 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.
#include "client_collaborator.h"
#include <gflags/gflags.h>
#include <deque>
#include <vector>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "log.h"
DEFINE_bool(buffer_profile_display, false,
"Show buffer collaborator latency HUD");
DEFINE_bool(editor_debug_display, false, "Show editor debug information");
absl::Mutex ClientCollaborator::mu_;
std::vector<ClientCollaborator*> ClientCollaborator::all_;
absl::Mutex Invalidator::mu_;
std::vector<Invalidator*> Invalidator::invalidators_;
Invalidator::Invalidator() {
absl::MutexLock lock(&mu_);
invalidators_.push_back(this);
}
Invalidator::~Invalidator() {
absl::MutexLock lock(&mu_);
invalidators_.erase(
std::remove(invalidators_.begin(), invalidators_.end(), this),
invalidators_.end());
}
void Invalidator::InvalidateAll() {
absl::MutexLock lock(&mu_);
for (auto p : invalidators_) p->Invalidate();
}
ClientCollaborator::ClientCollaborator(const Buffer* buffer)
: AsyncCollaborator("terminal", absl::Seconds(0), absl::Seconds(0)),
buffer_(buffer),
editor_(Editor::Make(buffer_->site(), buffer_->filename().string(),
!buffer->synthetic())),
recently_used_(false) {
absl::MutexLock lock(&mu_);
all_.push_back(this);
}
ClientCollaborator::~ClientCollaborator() {
absl::MutexLock lock(&mu_);
all_.erase(std::remove(all_.begin(), all_.end(), this), all_.end());
}
void ClientCollaborator::All_Render(RenderContainers containers, Theme* theme) {
absl::MutexLock lock(&mu_);
for (auto t : all_) t->Render(containers, theme);
}
void ClientCollaborator::Push(const EditNotification& notification) {
LogTimer tmr("term_push");
{
absl::MutexLock lock(&mu_);
tmr.Mark("lock");
editor_->UpdateState(&tmr, notification);
tmr.Mark("update");
}
Invalidator::InvalidateAll();
}
EditResponse ClientCollaborator::Pull() {
auto ready = [this]() {
mu_.AssertHeld();
return editor_->HasCommands() || recently_used_;
};
mu_.LockWhen(absl::Condition(&ready));
LogTimer tmr("term_pull");
EditResponse r = editor_->MakeResponse();
tmr.Mark("make");
r.become_used |= recently_used_;
recently_used_ = false;
mu_.Unlock();
tmr.Mark("unlock");
Invalidator::InvalidateAll();
return r;
}
void ClientCollaborator::Render(RenderContainers containers, Theme* theme) {
/*
* edit item
*/
editor_->Render(theme,
buffer_->synthetic() ? containers.side_bar : containers.main);
if (FLAGS_buffer_profile_display) {
containers.side_bar->MakeSimpleText(theme->ThemeToken({}, 0),
buffer_->ProfileData());
}
if (FLAGS_editor_debug_display) {
containers.side_bar->MakeSimpleText(theme->ThemeToken({}, 0),
editor_->DebugData());
}
#if 0
side_bar.AddItem(LAY_FILL, [this](TerminalRenderContext* context) {
absl::MutexLock lock(&mu_);
editor_->RenderSideBar(context);
});
#endif
#if 0
auto diagnostics = containers.ext_status->MakeColumn(
absl::StrCat("diagnostics:", buffer_->filename().string()));
int num_diagnostics = 0;
editor_->CurrentState().content.ForEachAttribute(
Attribute::kDiagnostic, [&](ID, const Attribute& attribute) {
if (num_diagnostics < 10) {
std::string msg = absl::StrCat(
"[", Diagnostic_Severity_Name(attribute.diagnostic().severity()),
"] ", attribute.diagnostic().message());
diagnostics
.AddItem(LAY_TOP | LAY_HFILL,
[msg](TerminalRenderContext* context) {
Log() << *context->window;
context->Put(0, 0, msg, context->color->Theme({}, 0));
})
.FixSize(0, 1);
num_diagnostics++;
}
});
#endif
}
CLIENT_COLLABORATOR(ClientCollaborator, buffer) { return true; }