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 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE 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 General Public License for more details.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
class ErrorListComp : public TreePanelBase,
private ChangeListener
{
public:
ErrorListComp (ErrorList& el)
: TreePanelBase (nullptr, String()),
errorList (el)
{
setName ("Errors and Warnings");
setEmptyTreeMessage ("(No Messages)");
tree.setMultiSelectEnabled (false);
tree.setRootItemVisible (false);
setRoot (new ErrorRootTreeItem (errorList));
errorList.addChangeListener (this);
errorListChanged();
}
~ErrorListComp()
{
errorList.removeChangeListener (this);
}
void errorListChanged()
{
static_cast<ErrorRootTreeItem*> (rootItem.get())->refreshSubItems();
}
void moveBy (const int delta)
{
if (delta < 0)
if (TreeViewItem* selected = tree.getSelectedItem (0))
if (selected->getRowNumberInTree() <= 1)
return;
tree.moveSelectedRow (delta);
if (dynamic_cast<ErrorMessageTreeItem*> (tree.getSelectedItem (0)) == nullptr)
tree.moveSelectedRow (delta);
}
void showNext() { moveBy (1); }
void showPrevious() { moveBy (-1); }
private:
TreeView list;
ErrorList& errorList;
struct ErrorMessageTreeItem;
void changeListenerCallback (ChangeBroadcaster*) override
{
errorListChanged();
}
static void limitNumberOfSubItems (TreeViewItem& item, const int maxSubItems)
{
while (item.getNumSubItems() > maxSubItems)
item.removeSubItem (item.getNumSubItems() - 1);
}
//==============================================================================
class ErrorRootTreeItem : public JucerTreeViewBase
{
public:
ErrorRootTreeItem (ErrorList& el) : errorList (el) {}
String getRenamingName() const override { return getDisplayName(); }
String getDisplayName() const override { return "Errors and Warnings"; }
void setName (const String&) override {}
bool isMissing() override { return false; }
Icon getIcon() const override { return Icon (getIcons().bug, getContrastingColour (0.8f)); }
bool canBeSelected() const override { return true; }
bool mightContainSubItems() override { return true; }
String getUniqueName() const override { return "errors"; }
void refreshSubItems()
{
Array<DiagnosticMessage> errors;
errorList.takeCopy (errors);
StringArray files;
for (const auto& m : errors)
{
files.addIfNotAlreadyThere (m.mainFile);
if (m.associatedDiagnostic != nullptr)
files.addIfNotAlreadyThere (m.associatedDiagnostic->mainFile);
}
limitNumberOfSubItems (*this, files.size());
int i = 0;
for (const auto& f : files)
{
if (i >= getNumSubItems() || static_cast<CompileUnitTreeItem*> (getSubItem (i))->compileUnit != f)
{
limitNumberOfSubItems (*this, i);
addSubItem (new CompileUnitTreeItem (f));
}
static_cast<CompileUnitTreeItem*> (getSubItem (i))->refresh (errors);
++i;
}
}
private:
ErrorList& errorList;
};
//==============================================================================
struct CompileUnitTreeItem : public JucerTreeViewBase
{
CompileUnitTreeItem (const String& filename) : compileUnit (filename) {}
String getRenamingName() const override { return getDisplayName(); }
String getDisplayName() const override { return File (compileUnit).exists() ? File (compileUnit).getFileName() : compileUnit; }
void setName (const String&) override {}
bool isMissing() override { return false; }
Icon getIcon() const override { return Icon (getIcons().bug, getContrastingColour (0.8f)); }
bool canBeSelected() const override { return true; }
bool mightContainSubItems() override { return true; }
String getUniqueName() const override { return String::toHexString (compileUnit.hashCode64()); }
void addSubItems() override {}
void showOverlays()
{
for (int i = 0; i < getNumSubItems(); ++i)
if (auto* e = dynamic_cast<ErrorMessageTreeItem*> (getSubItem (i)))
e->showOverlays();
}
ErrorMessageTreeItem* getItemForError (const DiagnosticMessage& m) const
{
for (int i = 0; i < getNumSubItems(); ++i)
if (auto* item = dynamic_cast<ErrorMessageTreeItem*> (getSubItem(i)))
if (item->message == m)
return item;
return nullptr;
}
void refresh (const Array<DiagnosticMessage>& allErrors)
{
clearSubItems();
for (const auto& error : allErrors)
if (error.mainFile == compileUnit && error.associatedDiagnostic == nullptr)
addSubItem (new ErrorMessageTreeItem (error));
for (const auto& error : allErrors)
if (error.mainFile == compileUnit && error.associatedDiagnostic != nullptr)
if (ErrorMessageTreeItem* parent = getItemForError (*error.associatedDiagnostic))
parent->addSubItem (new ErrorMessageTreeItem (error));
}
void showDocument() override
{
if (ProjectContentComponent* pcc = getProjectContentComponent())
if (File (compileUnit).exists())
pcc->showEditorForFile (File (compileUnit), true);
}
String compileUnit;
};
//==============================================================================
struct ErrorMessageTreeItem : public JucerTreeViewBase
{
ErrorMessageTreeItem (const DiagnosticMessage& m)
: message (m), itemHeight (14)
{
setOpenness (Openness::opennessClosed);
uniqueID << message.message << ':' << message.range.toString();
}
~ErrorMessageTreeItem()
{
overlay.deleteAndZero();
}
String getRenamingName() const override { return getDisplayName(); }
String getDisplayName() const override { return message.message; }
void setName (const String&) override {}
bool isMissing() override { return false; }
Icon getIcon() const override { return Icon (message.isNote() ? getIcons().info
: getIcons().warning, getTextColour()); }
bool canBeSelected() const override { return true; }
bool mightContainSubItems() override { return getNumSubItems() != 0; }
String getUniqueName() const override { return uniqueID; }
Component* createItemComponent() override { return new ErrorItemComponent (*this); }
struct ErrorItemComponent : public TreeItemComponent
{
ErrorItemComponent (ErrorMessageTreeItem& e) : TreeItemComponent (e) {}
void resized() override
{
TreeItemComponent::resized();
const int width = getWidth();
const int iconWidth = 25; // TODO: this shouldn't be a magic number
if (width > iconWidth)
static_cast<ErrorMessageTreeItem&> (item).updateTextLayout (getWidth() - 30 /* accounting for icon */);
}
void lookAndFeelChanged() override
{
resized();
}
};
void showPopupMenu() override
{
PopupMenu menu;
menu.addItem (1, "Copy");
launchPopupMenu (menu);
}
void handlePopupMenuResult (int resultCode) override
{
if (resultCode == 1)
SystemClipboard::copyTextToClipboard (message.toString());
}
void paintIcon (Graphics& g, Rectangle<int> area) override
{
getIcon().draw (g, area.toFloat(), isIconCrossedOut());
}
void paintContent (Graphics& g, const Rectangle<int>& area) override
{
text.draw (g, area.toFloat());
}
int getItemHeight() const override
{
return itemHeight;
}
Colour getTextColour() const
{
Colour bkg (getOwnerView()->findColour (mainBackgroundColourId));
return bkg.contrasting (message.isError() ? Colours::darkred
: message.isWarning() ? Colours::yellow.darker()
: Colours::grey, 0.4f);
}
void updateTextLayout (int width)
{
jassert (width >= 0);
AttributedString s (message.message);
s.setFont (Font (12.0f));
s.setColour (getTextColour());
text.createLayout (s, (float) width);
const int newHeight = 2 + jmax (14, (int) text.getHeight());
if (itemHeight != newHeight)
{
itemHeight = newHeight;
treeHasChanged();
}
}
SourceCodeEditor* getEditor()
{
if (ProjectContentComponent* pcc = getProjectContentComponent())
{
const File file (File::createFileWithoutCheckingPath (message.range.file));
if (message.range.isValid() && file.exists() && pcc->showEditorForFile (file, false))
{
if (SourceCodeEditor* ed = dynamic_cast<SourceCodeEditor*> (pcc->getEditorComponent()))
{
return ed;
}
}
}
return nullptr;
}
void showDocument() override
{
if (SourceCodeEditor* ed = getEditor())
{
ed->grabKeyboardFocus();
ed->highlight (message.range.range, false);
if (auto cu = findCompileUnitParent())
cu->showOverlays();
}
}
CompileUnitTreeItem* findCompileUnitParent()
{
for (TreeViewItem* p = getParentItem(); p != nullptr; p = p->getParentItem())
if (auto cu = dynamic_cast<CompileUnitTreeItem*> (p))
return cu;
return nullptr;
}
void showOverlays()
{
overlay.deleteAndZero();
if (ProjectContentComponent* pcc = getProjectContentComponent())
{
if (SourceCodeEditor* ed = dynamic_cast<SourceCodeEditor*> (pcc->getEditorComponent()))
{
auto start = CodeDocument::Position (ed->editor->getDocument(), message.range.range.getStart());
auto end = CodeDocument::Position (ed->editor->getDocument(), message.range.range.getEnd());
if (auto* ce = dynamic_cast<LiveBuildCodeEditor*> (ed->editor.get()))
overlay = ce->addDiagnosticOverlay (start, end, message.type);
}
}
for (int i = 0; i < getNumSubItems(); ++i)
if (auto* e = dynamic_cast<ErrorMessageTreeItem*> (getSubItem (i)))
e->showOverlays();
}
DiagnosticMessage message;
private:
String uniqueID;
TextLayout text;
int itemHeight;
Component::SafePointer<Component> overlay;
};
};
|