1
1
package raven .sqdev .editors ;
2
2
3
+ import java .io .ByteArrayInputStream ;
4
+ import java .io .IOException ;
5
+ import java .io .InputStream ;
3
6
import java .util .AbstractMap ;
4
7
import java .util .ArrayList ;
5
8
import java .util .List ;
9
+ import java .util .stream .Collectors ;
6
10
7
11
import org .antlr .v4 .runtime .tree .ParseTree ;
8
12
import org .eclipse .core .resources .IMarker ;
37
41
import raven .sqdev .interfaces .IMacroSupport ;
38
42
import raven .sqdev .interfaces .IManager ;
39
43
import raven .sqdev .interfaces .IMarkerSupport ;
44
+ import raven .sqdev .interfaces .IParseResult ;
40
45
import raven .sqdev .misc .CharacterPair ;
41
46
import raven .sqdev .misc .MultiPreferenceStore ;
42
47
import raven .sqdev .misc .SQDevInfobox ;
@@ -80,9 +85,9 @@ public class BasicCodeEditor extends TextEditor implements IMarkerSupport {
80
85
protected BasicSourceViewerConfiguration configuration ;
81
86
82
87
/**
83
- * The parse tree representing the input of this editor
88
+ * The parse result representing the input of this editor
84
89
*/
85
- protected ParseTree parseTree ;
90
+ protected IParseResult parseResult ;
86
91
/**
87
92
* The name of the rules used for parsing this editor's input
88
93
*/
@@ -142,8 +147,8 @@ public void setColorManager(ColorManager colorManager) {
142
147
143
148
@ Override
144
149
public ISourceViewer createSourceViewer (Composite parent , IVerticalRuler ruler , int styles ) {
145
- ISourceViewer viewer = new ProjectionViewer (parent , ruler , getOverviewRuler (),
146
- isOverviewRulerVisible (), styles );
150
+ ISourceViewer viewer = new ProjectionViewer (parent , ruler , getOverviewRuler (), isOverviewRulerVisible (),
151
+ styles );
147
152
148
153
getSourceViewerDecorationSupport (viewer );
149
154
@@ -158,8 +163,7 @@ public ISourceViewer createSourceViewer(Composite parent, IVerticalRuler ruler,
158
163
}
159
164
160
165
// add parse listener
161
- getBasicProvider ().getDocument (getEditorInput ())
162
- .addDocumentListener (new BasicParseTimeListener (this ));
166
+ getBasicProvider ().getDocument (getEditorInput ()).addDocumentListener (new BasicParseTimeListener (this ));
163
167
164
168
return viewer ;
165
169
}
@@ -175,13 +179,11 @@ protected void configureSourceViewerDecorationSupport(SourceViewerDecorationSupp
175
179
176
180
// character pair matching
177
181
support .setCharacterPairMatcher (matcher );
178
- support .setMatchingCharacterPainterPreferenceKeys (
179
- SQDevPreferenceConstants .SQDEV_EDITOR_MATCHING_BRACKETS_KEY ,
182
+ support .setMatchingCharacterPainterPreferenceKeys (SQDevPreferenceConstants .SQDEV_EDITOR_MATCHING_BRACKETS_KEY ,
180
183
SQDevPreferenceConstants .SQDEV_EDITOR_MATCHING_BRACKETS_COLOR_KEY );
181
184
182
185
// newLine highlighting
183
- support .setCursorLinePainterPreferenceKeys (
184
- SQDevPreferenceConstants .SQDEV_EDITOR_HIGHLIGHT_CURRENTLINE_KEY ,
186
+ support .setCursorLinePainterPreferenceKeys (SQDevPreferenceConstants .SQDEV_EDITOR_HIGHLIGHT_CURRENTLINE_KEY ,
185
187
SQDevPreferenceConstants .SQDEV_EDITOR_HIGHLIGHT_CURRENTLINE_COLOR_KEY );
186
188
187
189
}
@@ -253,8 +255,7 @@ public void createPartControl(Composite parent) {
253
255
// infrastructure for code folding
254
256
ProjectionViewer viewer = (ProjectionViewer ) getSourceViewer ();
255
257
256
- ProjectionSupport projectionSupport = new ProjectionSupport (viewer , getAnnotationAccess (),
257
- getSharedColors ());
258
+ ProjectionSupport projectionSupport = new ProjectionSupport (viewer , getAnnotationAccess (), getSharedColors ());
258
259
259
260
projectionSupport .install ();
260
261
@@ -266,8 +267,7 @@ public void createPartControl(Composite parent) {
266
267
// combine the SQDev PreferenceStore with the editor's one
267
268
268
269
// use the SQDev preferenceStore as the baseStore
269
- MultiPreferenceStore multiStore = new MultiPreferenceStore (
270
- SQDevPreferenceUtil .getPreferenceStore ());
270
+ MultiPreferenceStore multiStore = new MultiPreferenceStore (SQDevPreferenceUtil .getPreferenceStore ());
271
271
272
272
// add the editor's preferenceStore if available
273
273
IPreferenceStore editorStore = this .getPreferenceStore ();
@@ -341,13 +341,13 @@ public BasicDocumentProvider getBasicProvider() {
341
341
}
342
342
343
343
/**
344
- * Gets the <code>ParseTree</code> representing the input of this editor
344
+ * Gets the {@linkplain IParseResult} representing the input of this editor
345
345
*
346
- * @return The <code>ParseTree</code> or <code>null</code> if none has been set
346
+ * @return The result or <code>null</code> if none has been set
347
347
* so far
348
348
*/
349
- public ParseTree getParseTree () {
350
- return parseTree ;
349
+ public IParseResult getParseResult () {
350
+ return parseResult ;
351
351
}
352
352
353
353
/**
@@ -360,8 +360,8 @@ public List<String> getParseRuleNames() {
360
360
}
361
361
362
362
/**
363
- * This is a helper method that will do the parsing for the given input wihtout
364
- * any checks (whetehr there is an active parsing job) and in the same thread as
363
+ * This is a helper method that will do the parsing for the given input without
364
+ * any checks (whether there is an active parsing job) and in the same thread as
365
365
* it is called
366
366
*
367
367
* @param input
@@ -370,7 +370,7 @@ public List<String> getParseRuleNames() {
370
370
*/
371
371
private IStatus startParsingInput (String input ) {
372
372
// preprocess
373
- doPreprocessorParsing (input );
373
+ doPreprocessorParsing (new ByteArrayInputStream ( input . getBytes ()) );
374
374
375
375
// check if this parsing should be cancelled
376
376
synchronized (parsingIsCancelled ) {
@@ -381,7 +381,7 @@ private IStatus startParsingInput(String input) {
381
381
}
382
382
383
383
// parse
384
- ParseTree output = doParse (input );
384
+ IParseResult output = doParse (new ByteArrayInputStream ( input . getBytes ()) );
385
385
386
386
// check if this parsing should be cancelled
387
387
synchronized (parsingIsCancelled ) {
@@ -391,14 +391,17 @@ private IStatus startParsingInput(String input) {
391
391
}
392
392
}
393
393
394
- if (output == null || output .getChildCount () == 0 ) {
394
+ if (output == null
395
+ || output .getMarkers ().stream ().filter ((element ) -> element .getSeverity () == IMarker .SEVERITY_ERROR )
396
+ .collect (Collectors .toList ()).size () > 0 ) {
397
+ // don't process the parse tree if errors came up during lexing/parsing
395
398
applyParseChanges ();
396
399
397
400
return Status .CANCEL_STATUS ;
398
401
} else {
399
- parseTree = output ;
402
+ parseResult = output ;
400
403
401
- if (!processParseTree (parseTree )) {
404
+ if (!processParseTree (parseResult )) {
402
405
applyParseChanges ();
403
406
}
404
407
@@ -433,30 +436,29 @@ public boolean parseInput(boolean suspend) {
433
436
return false ;
434
437
}
435
438
436
- String input = document .get ();
439
+ String content = document .get ();
437
440
438
- if (input == null ) {
441
+ if (content == null ) {
439
442
return false ;
440
443
}
441
444
442
445
synchronized (parsingIsCancelled ) {
443
446
if (parsingIsCancelled && (parseJob == null || parseJob .getResult () != null )) {
444
447
// There is no other parsing in progress that should be
445
- // cancelled and cancelling is only possible after having
448
+ // cancelled and canceling is only possible after having
446
449
// initialized it
447
450
parsingIsCancelled = false ;
448
451
}
449
452
}
450
453
451
454
if (parseJob != null && parseJob .getState () != Job .NONE ) {
452
- // Ther previous Job is still running -> reschedule
455
+ // The previous Job is still running -> reschedule
453
456
parseJob .addJobChangeListener (new JobChangeAdapter () {
454
457
455
458
@ Override
456
459
public void done (IJobChangeEvent event ) {
457
460
// As there has been a request to parse the input again
458
- // do
459
- // it now as the old parsing process is finished
461
+ // do it now as the old parsing process is finished
460
462
parseInput ();
461
463
}
462
464
});
@@ -465,13 +467,13 @@ public void done(IJobChangeEvent event) {
465
467
}
466
468
467
469
if (suspend ) {
468
- startParsingInput (input );
470
+ startParsingInput (content );
469
471
} else {
470
472
parseJob = new Job ("Parsing \" " + getEditorInput ().getName () + "\" ..." ) {
471
473
472
474
@ Override
473
475
protected IStatus run (IProgressMonitor monitor ) {
474
- return startParsingInput (input );
476
+ return startParsingInput (content );
475
477
}
476
478
};
477
479
@@ -514,10 +516,17 @@ public void cancelParsing() {
514
516
* and sets the found macros if this editor is an instance of
515
517
* <code>IMacroSupport</code>.
516
518
*/
517
- protected void doPreprocessorParsing (String input ) {
519
+ protected void doPreprocessorParsing (InputStream input ) {
518
520
if (this instanceof IMacroSupport && getEditorInput () instanceof IFileEditorInput ) {
519
- PreprocessorParseResult result = ParseUtil .parseAndValidatePreprocess (input ,
520
- ((IFileEditorInput ) getEditorInput ()).getFile ().getLocation ());
521
+ PreprocessorParseResult result ;
522
+ try {
523
+ result = ParseUtil .parseAndValidatePreprocess (input ,
524
+ ((IFileEditorInput ) getEditorInput ()).getFile ().getLocation ());
525
+ } catch (IOException e ) {
526
+ e .printStackTrace ();
527
+ createMarker (IMarker .PROBLEM , 0 , 0 , "Unable to preprocess the file" , IMarker .SEVERITY_ERROR );
528
+ return ;
529
+ }
521
530
522
531
((IMacroSupport ) this ).setMacros (result .getMacros (), true );
523
532
@@ -533,18 +542,18 @@ protected void doPreprocessorParsing(String input) {
533
542
* Note: You might want to call {@link #applyParseChanges()} after the
534
543
* processing
535
544
*
536
- * @param tree
537
- * The generated tree
545
+ * @param parseResult
546
+ * The generated parse result
538
547
* @return Whether this function has called {@link #applyParseChanges()}. If not
539
548
* the default implementation of {@link #parseInput()} will call this
540
549
* function afterwards.
541
550
*/
542
- protected boolean processParseTree (ParseTree parseTree ) {
551
+ protected boolean processParseTree (IParseResult parseResult ) {
543
552
return false ;
544
553
}
545
554
546
555
/**
547
- * Parses the input of this editor in order to set the {@link #parseTree } for
556
+ * Parses the input of this editor in order to set the {@link #parseResult } for
548
557
* this editor. <br>
549
558
* Note: You might want to call {@link #applyParseChanges()} after parsing (or
550
559
* rather after {@link #processParseTree(ParseTree)}.<br>
@@ -559,7 +568,7 @@ protected boolean processParseTree(ParseTree parseTree) {
559
568
* parsing failed (if not overridden by subclasses this method always
560
569
* returns <code>null</code>
561
570
*/
562
- protected ParseTree doParse (String input ) {
571
+ protected IParseResult doParse (InputStream input ) {
563
572
// parsing diabled
564
573
return null ;
565
574
}
@@ -573,8 +582,7 @@ protected ParseTree doParse(String input) {
573
582
*/
574
583
protected void createManagers (List <IManager > managerList ) {
575
584
// add folding manager
576
- managerList .add (new BasicFoldingManager (
577
- ((ProjectionViewer ) getSourceViewer ()).getProjectionAnnotationModel ()));
585
+ managerList .add (new BasicFoldingManager (((ProjectionViewer ) getSourceViewer ()).getProjectionAnnotationModel ()));
578
586
// add marker manager
579
587
managerList .add (new BasicMarkerManager (this ));
580
588
}
@@ -598,8 +606,8 @@ public void createMarker(String type, int offset, int length, String message, in
598
606
}
599
607
}
600
608
601
- ((BasicMarkerManager ) getManager (BasicMarkerManager .TYPE )).addMarker (type , line , offset , length ,
602
- severity , message );
609
+ ((BasicMarkerManager ) getManager (BasicMarkerManager .TYPE )).addMarker (type , line , offset , length , severity ,
610
+ message );
603
611
}
604
612
605
613
@ Override
@@ -651,8 +659,8 @@ public void addFoldingArea(Position position) {
651
659
652
660
// don't fold if the code is only one line long
653
661
try {
654
- if (doc == null || doc . getLineOfOffset ( position . offset ) == doc
655
- .getLineOfOffset (position .offset + position .length )) {
662
+ if (doc == null
663
+ || doc . getLineOfOffset ( position . offset ) == doc .getLineOfOffset (position .offset + position .length )) {
656
664
return ;
657
665
}
658
666
} catch (BadLocationException e ) {
@@ -666,15 +674,14 @@ public void addFoldingArea(Position position) {
666
674
667
675
ProjectionAnnotation annotation = new ProjectionAnnotation ();
668
676
669
- BasicFoldingManager foldingManager = (BasicFoldingManager ) getManager (
670
- BasicFoldingManager .getManagerType ());
677
+ BasicFoldingManager foldingManager = (BasicFoldingManager ) getManager (BasicFoldingManager .getManagerType ());
671
678
672
679
if (foldingManager == null ) {
673
680
return ;
674
681
}
675
682
676
- foldingManager . addFoldingArea (
677
- new AbstractMap .SimpleEntry <ProjectionAnnotation , Position >(annotation , position ));
683
+ foldingManager
684
+ . addFoldingArea ( new AbstractMap .SimpleEntry <ProjectionAnnotation , Position >(annotation , position ));
678
685
}
679
686
680
687
/**
0 commit comments