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 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2017 - ROLI Ltd.
JUCE is an open source library subject to commercial or open-source
licensing.
By using JUCE, you agree to the terms of both the JUCE 5 End-User License
Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
27th April 2017).
End User License Agreement: www.juce.com/juce-5-licence
Privacy Policy: www.juce.com/juce-5-privacy-policy
Or: You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================
*/
#pragma once
class LiveBuildCodeEditorDocument;
//==============================================================================
class LiveBuildCodeEditor : public CppCodeEditorComponent,
private Timer
{
public:
LiveBuildCodeEditor (LiveBuildCodeEditorDocument& edDoc, CodeDocument& doc)
: CppCodeEditorComponent (edDoc.getFile(), doc),
editorDoc (edDoc),
classList (*this, edDoc)
{
}
~LiveBuildCodeEditor()
{
for (int i = getNumChildComponents(); --i >= 0;)
if (auto* c = dynamic_cast<DiagnosticOverlayComponent*> (getChildComponent (i)))
delete c;
}
CompileEngineChildProcess::Ptr getChildProcess() const
{
return editorDoc.getChildProcess();
}
Component* addDiagnosticOverlay (CodeDocument::Position start, CodeDocument::Position end,
DiagnosticMessage::Type diagType)
{
auto* d = new DiagnosticOverlayComponent (*this, start, end, diagType);
addAndMakeVisible (d);
return d;
}
private:
LiveBuildCodeEditorDocument& editorDoc;
//==============================================================================
struct OverlayComponent : public Component,
private GenericCodeEditorComponent::Listener,
private CodeDocument::Listener
{
OverlayComponent (CodeDocument::Position start,
CodeDocument::Position end)
: startPosition (start),
endPosition (end)
{
startPosition.setPositionMaintained (true);
endPosition.setPositionMaintained (true);
}
~OverlayComponent()
{
setEditor (nullptr);
}
void setEditor (GenericCodeEditorComponent* editor)
{
if (editor != codeEditor)
{
if (codeEditor != nullptr)
{
codeEditor->removeListener (this);
codeEditor->getDocument().removeListener (this);
codeEditor->removeChildComponent (this);
}
codeEditor = editor;
if (codeEditor != nullptr)
{
codeEditor->addListener (this);
codeEditor->getDocument().addListener (this);
codeEditor->addAndMakeVisible (this);
}
if (editor != nullptr)
updatePosition();
}
}
void codeEditorViewportMoved (CodeEditorComponent& editor) override
{
setEditor (dynamic_cast<GenericCodeEditorComponent*> (&editor));
updatePosition();
}
void codeDocumentTextInserted (const String&, int) override { updatePosition(); }
void codeDocumentTextDeleted (int, int) override { updatePosition(); }
void parentSizeChanged() override
{
updatePosition();
}
virtual void updatePosition() = 0;
Component::SafePointer<GenericCodeEditorComponent> codeEditor;
CodeDocument::Position startPosition, endPosition;
};
//==============================================================================
struct LaunchClassOverlayComponent : public OverlayComponent
{
LaunchClassOverlayComponent (GenericCodeEditorComponent& editor,
CodeDocument::Position start, CodeDocument::Position end,
const String className)
: OverlayComponent (start, end),
launchButton (className.fromLastOccurrenceOf ("::", false, false)),
name (className)
{
setAlwaysOnTop (true);
setEditor (&editor);
addAndMakeVisible (launchButton);
}
void updatePosition() override
{
if (codeEditor != nullptr)
{
jassert (isVisible());
const auto charArea = codeEditor->getCharacterBounds (startPosition);
const int height = charArea.getHeight() + 8;
Font f (height * 0.7f);
const int width = jmin (height * 2 + f.getStringWidth (launchButton.getName()),
jmax (120, codeEditor->proportionOfWidth (0.2f)));
setBounds (codeEditor->getWidth() - width - 10, charArea.getY() - 4,
width, height);
}
}
void resized() override
{
launchButton.setBounds (getLocalBounds());
}
struct LaunchButton : public Button
{
LaunchButton (const String& nm) : Button (nm)
{
setMouseCursor (MouseCursor::PointingHandCursor);
}
void paintButton (Graphics& g, bool isMouseOverButton, bool isButtonDown) override
{
Colour background (findColour (CodeEditorComponent::backgroundColourId)
.contrasting()
.overlaidWith (Colours::yellow.withAlpha (0.5f))
.withAlpha (0.4f));
g.setColour (background);
g.fillRoundedRectangle (getLocalBounds().toFloat(), 3.0f);
const Path& path = getIcons().play;
Colour col (background.contrasting (Colours::lightgreen, 0.6f));
Rectangle<int> r (getLocalBounds().reduced (getHeight() / 5));
Icon (path, col.withAlpha (isButtonDown ? 1.0f : (isMouseOverButton ? 0.8f : 0.5f)))
.draw (g, r.removeFromLeft (getHeight()).toFloat(), false);
g.setColour (Colours::white);
g.setFont (getHeight() * 0.7f);
g.drawFittedText (getName(), r, Justification::centredLeft, 1);
}
void clicked() override
{
if (auto* l = findParentComponentOfClass<LaunchClassOverlayComponent>())
l->launch();
}
};
void launch()
{
if (auto* e = findParentComponentOfClass<LiveBuildCodeEditor>())
e->launch (name);
}
private:
LaunchButton launchButton;
String name;
};
struct ComponentClassList : private Timer
{
ComponentClassList (GenericCodeEditorComponent& e, LiveBuildCodeEditorDocument& edDoc)
: owner (e),
childProcess (edDoc.getChildProcess()),
file (edDoc.getFile())
{
startTimer (600);
}
~ComponentClassList()
{
deleteOverlays();
}
void timerCallback() override
{
Array<ClassDatabase::Class*> newClasses;
if (childProcess != nullptr)
const_cast <ClassDatabase::ClassList&> (childProcess->getComponentList()).globalNamespace.findClassesDeclaredInFile (newClasses, file);
for (int i = newClasses.size(); --i >= 0;)
if (! newClasses.getUnchecked(i)->getInstantiationFlags().canBeInstantiated())
newClasses.remove (i);
if (newClasses != classes)
{
classes = newClasses;
deleteOverlays();
for (auto& c : classes)
{
CodeDocument::Position pos (owner.getDocument(), c->getClassDeclarationRange().range.getStart());
overlays.add (new LaunchClassOverlayComponent (owner, pos, pos, c->getName()));
}
}
}
void deleteOverlays()
{
for (auto& o : overlays)
o.deleteAndZero();
overlays.clear();
}
GenericCodeEditorComponent& owner;
CompileEngineChildProcess::Ptr childProcess;
File file;
Array<ClassDatabase::Class*> classes;
Array<Component::SafePointer<Component>> overlays;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ComponentClassList)
};
ComponentClassList classList;
//==============================================================================
struct DiagnosticOverlayComponent : public OverlayComponent
{
DiagnosticOverlayComponent (GenericCodeEditorComponent& editor,
CodeDocument::Position start, CodeDocument::Position end,
DiagnosticMessage::Type diagType)
: OverlayComponent (start, end), diagnosticType (diagType)
{
setInterceptsMouseClicks (false, false);
setEditor (&editor);
}
void updatePosition() override
{
if (codeEditor != nullptr)
{
jassert (isVisible());
const auto charStartRect = codeEditor->getCharacterBounds (startPosition);
const auto charEndRect = codeEditor->getCharacterBounds (endPosition);
auto charHeight = charStartRect.getHeight();
const auto editorBounds = codeEditor->getBounds();
arrowXMin = static_cast<int> (jmin (charStartRect.getX(), charEndRect.getX()));
arrowXMax = static_cast<int> (jmax (charStartRect.getX() + charStartRect.getWidth(), charEndRect.getX() + charEndRect.getWidth()));
lineYMin = charStartRect.getY();
lineOffset = charHeight;
setBounds (0, lineYMin, editorBounds.getWidth(), lineOffset + charHeight);
repaint();
}
}
void paint (Graphics& g) override
{
const auto diagColour = diagnosticType == DiagnosticMessage::Type::error ? Colours::red
: Colour (200, 200, 64);
g.setColour (diagColour.withAlpha (0.2f));
g.fillRect (getLocalBounds().withTrimmedBottom (lineOffset));
Path path;
const float bottomY = getHeight() - (lineOffset / 2.0f);
path.addTriangle ((float) arrowXMin, bottomY,
(arrowXMax + arrowXMin) / 2.0f, (float) lineOffset,
(float) arrowXMax, bottomY);
g.setColour (diagColour.withAlpha (0.8f));
g.fillPath (path);
}
private:
int arrowXMin, arrowXMax;
int lineYMin, lineOffset;
const DiagnosticMessage::Type diagnosticType;
};
//==============================================================================
void timerCallback() override
{
if (isMouseButtonDownAnywhere())
return;
MouseInputSource mouse = Desktop::getInstance().getMainMouseSource();
Component* underMouse = mouse.getComponentUnderMouse();
if (underMouse != nullptr
&& (dynamic_cast<ControlsComponent*> (underMouse) != nullptr
|| underMouse->findParentComponentOfClass<ControlsComponent>() != nullptr))
return;
overlay.reset();
if (hasKeyboardFocus (true) && underMouse != nullptr
&& (underMouse == this || underMouse->isParentOf (this)))
{
Point<int> mousePos = getLocalPoint (nullptr, mouse.getScreenPosition()).toInt();
CodeDocument::Position start, end;
getDocument().findTokenContaining (getPositionAt (mousePos.x, mousePos.y), start, end);
if (end.getPosition() > start.getPosition())
{
Range<int> selection = optimiseSelection ({ start.getPosition(), end.getPosition() });
String text = getTextInRange (selection).toLowerCase();
if (isIntegerLiteral (text) || isFloatLiteral (text))
overlay.reset (new LiteralHighlightOverlay (*this, selection, mightBeColourValue (text)));
}
}
startTimerHz (10);
}
void hideOverlay()
{
stopTimer();
overlay.reset();
}
void focusLost (FocusChangeType) override
{
if (CompileEngineChildProcess::Ptr childProcess = getChildProcess())
childProcess->flushEditorChanges();
}
void mouseMove (const MouseEvent& e) override
{
if (overlay == nullptr)
startTimer (100);
CppCodeEditorComponent::mouseMove (e);
}
void mouseDrag (const MouseEvent& e) override
{
if (e.getDistanceFromDragStart() > 0)
hideOverlay();
CppCodeEditorComponent::mouseDrag (e);
}
void mouseDown (const MouseEvent& e) override
{
CppCodeEditorComponent::mouseDown (e);
}
void mouseUp (const MouseEvent& e) override
{
CppCodeEditorComponent::mouseUp (e);
}
bool keyPressed (const KeyPress& key) override
{
hideOverlay();
return CppCodeEditorComponent::keyPressed (key);
}
static bool isIntegerLiteral (const String& text) { return CppParserHelpers::parseSingleToken (text) == CPlusPlusCodeTokeniser::tokenType_integer; }
static bool isFloatLiteral (const String& text) { return CppParserHelpers::parseSingleToken (text) == CPlusPlusCodeTokeniser::tokenType_float; }
static bool mightBeColourValue (const String& text)
{
return isIntegerLiteral (text)
&& text.trim().startsWith ("0x")
&& text.trim().length() > 7;
}
Range<int> optimiseSelection (Range<int> selection)
{
String text (getTextInRange (selection));
if (CharacterFunctions::isDigit (text[0]) || text[0] == '.')
if (getTextInRange (Range<int> (selection.getStart() - 1, selection.getStart())) == "-")
selection.setStart (selection.getStart() - 1);
selection.setStart (selection.getStart() + (text.length() - text.trimStart().length()));
selection.setEnd (selection.getEnd() - (text.length() - text.trimEnd().length()));
return selection;
}
void launch (const String& name)
{
if (CompileEngineChildProcess::Ptr p = getChildProcess())
if (auto* cls = p->getComponentList().globalNamespace.findClass (name))
p->openPreview (*cls);
}
//==============================================================================
class ControlsComponent : public Component,
private ChangeListener
{
public:
ControlsComponent (CodeDocument& doc, const Range<int>& selection,
CompileEngineChildProcess::Ptr cp, bool showColourSelector)
: document (doc),
start (doc, selection.getStart()),
end (doc, selection.getEnd()),
childProcess (cp)
{
slider.setTextBoxStyle (Slider::NoTextBox, true, 0, 0);
slider.setWantsKeyboardFocus (false);
slider.setMouseClickGrabsKeyboardFocus (false);
setWantsKeyboardFocus (false);
setMouseClickGrabsKeyboardFocus (false);
addAndMakeVisible (&slider);
updateRange();
slider.onValueChange = [this] { updateSliderValue(); };
slider.onDragEnd = [this] { updateRange(); };
if (showColourSelector)
{
updateColourSelector();
selector.setWantsKeyboardFocus (false);
selector.setMouseClickGrabsKeyboardFocus (false);
addAndMakeVisible (&selector);
setSize (400, sliderHeight + 400);
selector.addChangeListener (this);
}
else
{
setSize (400, sliderHeight);
}
end.setPositionMaintained (true);
}
void updateRange()
{
double v = getValue();
if (isFloat())
slider.setRange (v - 10, v + 10);
else
slider.setRange (v - 100, v + 100);
slider.setValue (v, dontSendNotification);
}
private:
Slider slider;
ColourSelector selector;
CodeDocument& document;
CodeDocument::Position start, end;
CompileEngineChildProcess::Ptr childProcess;
static const int sliderHeight = 26;
void paint (Graphics& g) override
{
g.setColour (LiteralHighlightOverlay::getBackgroundColour());
g.fillRoundedRectangle (getLocalBounds().toFloat(), 8.0f);
}
void updateSliderValue()
{
const String oldText (document.getTextBetween (start, end));
const String newText (CppParserHelpers::getReplacementStringInSameFormat (oldText, slider.getValue()));
if (oldText != newText)
document.replaceSection (start.getPosition(), end.getPosition(), newText);
if (childProcess != nullptr)
childProcess->flushEditorChanges();
updateColourSelector();
}
void changeListenerCallback (ChangeBroadcaster*) override
{
setNewColour (selector.getCurrentColour());
}
void updateColourSelector()
{
selector.setCurrentColour (getCurrentColour());
}
Colour getCurrentColour() const
{
int64 val;
if (CppParserHelpers::parseInt (document.getTextBetween (start, end), val))
return Colour ((uint32) val);
return Colours::white;
}
void setNewColour (const Colour& c)
{
const String oldText (document.getTextBetween (start, end));
const String newText (CppParserHelpers::getReplacementStringInSameFormat (oldText, (int64) c.getARGB()));
if (oldText != newText)
document.replaceSection (start.getPosition(), end.getPosition(), newText);
if (childProcess != nullptr)
childProcess->flushEditorChanges();
}
void resized() override
{
Rectangle<int> r (getLocalBounds());
slider.setBounds (r.removeFromTop (sliderHeight));
r.removeFromTop (10);
if (selector.isVisible())
selector.setBounds (r);
}
double getValue() const
{
const String text (document.getTextBetween (start, end));
if (text.containsChar ('.'))
{
double f;
if (CppParserHelpers::parseFloat (text, f))
return f;
}
else
{
int64 val;
if (CppParserHelpers::parseInt (text, val))
return (double) val;
}
jassertfalse;
return 0;
}
bool isFloat() const
{
return document.getTextBetween (start, end).containsChar ('.');
}
};
//==============================================================================
struct LiteralHighlightOverlay : public Component,
private CodeDocument::Listener
{
LiteralHighlightOverlay (LiveBuildCodeEditor& e, Range<int> section, bool showColourSelector)
: owner (e),
start (e.getDocument(), section.getStart()),
end (e.getDocument(), section.getEnd()),
controls (e.getDocument(), section, e.getChildProcess(), showColourSelector)
{
if (e.hasKeyboardFocus (true))
previouslyFocused = Component::getCurrentlyFocusedComponent();
start.setPositionMaintained (true);
end.setPositionMaintained (true);
setInterceptsMouseClicks (false, false);
if (Component* parent = owner.findParentComponentOfClass<ProjectContentComponent>())
parent->addAndMakeVisible (controls);
else
jassertfalse;
owner.addAndMakeVisible (this);
toBack();
updatePosition();
owner.getDocument().addListener (this);
}
~LiteralHighlightOverlay()
{
if (auto* p = getParentComponent())
{
p->removeChildComponent (this);
if (previouslyFocused != nullptr && ! previouslyFocused->hasKeyboardFocus (true))
previouslyFocused->grabKeyboardFocus();
}
owner.getDocument().removeListener (this);
}
void paint (Graphics& g) override
{
g.setColour (getBackgroundColour());
Rectangle<int> r (getLocalBounds());
g.fillRect (r.removeFromTop (borderSize));
g.fillRect (r.removeFromLeft (borderSize));
g.fillRect (r.removeFromRight (borderSize));
}
void updatePosition()
{
Rectangle<int> area = owner.getCharacterBounds (start)
.getUnion (owner.getCharacterBounds (end.movedBy (-1)))
.expanded (borderSize)
.withTrimmedBottom (borderSize);
setBounds (getParentComponent()->getLocalArea (&owner, area));
area.setPosition (area.getX() - controls.getWidth() / 2, area.getBottom());
area.setSize (controls.getWidth(), controls.getHeight());
controls.setBounds (controls.getParentComponent()->getLocalArea (&owner, area));
}
void codeDocumentTextInserted (const String&, int) override { updatePosition(); }
void codeDocumentTextDeleted (int, int) override { updatePosition(); }
LiveBuildCodeEditor& owner;
CodeDocument::Position start, end;
ControlsComponent controls;
Component::SafePointer<Component> previouslyFocused;
static const int borderSize = 4;
static Colour getBackgroundColour() { return Colour (0xcb5c7879); }
};
std::unique_ptr<LiteralHighlightOverlay> overlay;
};
//==============================================================================
class LiveBuildCodeEditorDocument : public SourceCodeDocument
{
public:
LiveBuildCodeEditorDocument (Project* project, const File& file)
: SourceCodeDocument (project, file)
{
if (project != nullptr)
if (CompileEngineChildProcess::Ptr childProcess = getChildProcess())
childProcess->editorOpened (file, getCodeDocument());
}
struct Type : public SourceCodeDocument::Type
{
Document* openFile (Project* proj, const File& file) override
{
return new LiveBuildCodeEditorDocument (proj, file);
}
};
Component* createEditor() override
{
SourceCodeEditor* e = nullptr;
if (fileNeedsCppSyntaxHighlighting (getFile()))
e = new SourceCodeEditor (this, new LiveBuildCodeEditor (*this, getCodeDocument()));
else
e = new SourceCodeEditor (this, getCodeDocument());
applyLastState (*(e->editor));
return e;
}
// override save() to make a few more attempts at saving if it fails, since on Windows
// the compiler can interfere with things saving..
bool save() override
{
for (int i = 5; --i >= 0;)
{
if (SourceCodeDocument::save()) // should already re-try for up to half a second
return true;
Thread::sleep (100);
}
return false;
}
CompileEngineChildProcess::Ptr getChildProcess() const
{
return ProjucerApplication::getApp().childProcessCache->getExisting (*project);
}
};
|