Пример #1
0
 public Style(StyleKind styleKind, Color?color = null, Color?background = null, FontFlags fontFlags = 0)
 {
     StyleKind  = styleKind;
     Color      = color;
     Background = background;
     FontFlags  = fontFlags;
 }
Пример #2
0
 public Style? this[StyleKind styleKind]
 => Styles.TryGetValue(styleKind, out Style style) ? (Style?)style : null;
Пример #3
0
        /// <summary>
        /// Parses the bytes to retrieve a StyleSheetDescription
        /// </summary>
        /// <param name="bytes">The bytes</param>
        /// <param name="cbStdBase"></param>
        /// <param name="dataStream">The "Data" stream (optional, can be null)</param>
        public StyleSheetDescription(byte[] bytes, int cbStdBase, VirtualStream dataStream)
        {
            BitArray bits = new BitArray(bytes);

            //parsing the base (fix part)

            if (cbStdBase >= 2)
            {
                //sti
                BitArray stiBits = Utils.BitArrayCopy(bits, 0, 12);
                this.sti = (StyleIdentifier)Utils.BitArrayToUInt32(stiBits);
                //flags
                this.fScratch     = bits[12];
                this.fInvalHeight = bits[13];
                this.fHasUpe      = bits[14];
                this.fMassCopy    = bits[15];
            }
            if (cbStdBase >= 4)
            {
                //stk
                BitArray stkBits = Utils.BitArrayCopy(bits, 16, 4);
                this.stk = (StyleKind)Utils.BitArrayToUInt32(stkBits);
                //istdBase
                BitArray istdBits = Utils.BitArrayCopy(bits, 20, 12);
                this.istdBase = (UInt32)Utils.BitArrayToUInt32(istdBits);
            }
            if (cbStdBase >= 6)
            {
                //cupx
                BitArray cupxBits = Utils.BitArrayCopy(bits, 32, 4);
                this.cupx = (UInt16)Utils.BitArrayToUInt32(cupxBits);
                //istdNext
                BitArray istdNextBits = Utils.BitArrayCopy(bits, 36, 12);
                this.istdNext = (UInt32)Utils.BitArrayToUInt32(istdNextBits);
            }
            if (cbStdBase >= 8)
            {
                //bchUpe
                BitArray bchBits = Utils.BitArrayCopy(bits, 48, 16);
                this.bchUpe = (UInt16)Utils.BitArrayToUInt32(bchBits);
            }
            if (cbStdBase >= 10)
            {
                //flags
                this.fAutoRedef       = bits[64];
                this.fHidden          = bits[65];
                this.f97LidsSet       = bits[66];
                this.fCopyLang        = bits[67];
                this.fPersonalCompose = bits[68];
                this.fPersonalReply   = bits[69];
                this.fPersonal        = bits[70];
                this.fNoHtmlExport    = bits[71];
                this.fSemiHidden      = bits[72];
                this.fLocked          = bits[73];
                this.fInternalUse     = bits[74];
            }
            if (cbStdBase >= 12)
            {
                //istdLink
                BitArray istdLinkBits = Utils.BitArrayCopy(bits, 80, 12);
                this.istdLink = (UInt32)Utils.BitArrayToUInt32(istdLinkBits);
                //fHasOriginalStyle
                this.fHasOriginalStyle = bits[92];
            }
            if (cbStdBase >= 16)
            {
                //rsid
                BitArray rsidBits = Utils.BitArrayCopy(bits, 96, 32);
                this.rsid = Utils.BitArrayToUInt32(rsidBits);
            }

            //parsing the variable part

            //xstz
            byte characterCount = bytes[cbStdBase];

            //characters are zero-terminated, so 1 char has 2 bytes:
            byte[] name = new byte[characterCount * 2];
            Array.Copy(bytes, cbStdBase + 2, name, 0, name.Length);
            //remove zero-termination
            this.xstzName = Encoding.Unicode.GetString(name);

            //parse the UPX structs
            int upxOffset = cbStdBase + 1 + (characterCount * 2) + 2;

            for (int i = 0; i < this.cupx; i++)
            {
                //find the next even byte border
                if (upxOffset % 2 != 0)
                {
                    upxOffset++;
                }

                //get the count of bytes for UPX
                UInt16 cbUPX = System.BitConverter.ToUInt16(bytes, upxOffset);

                if (cbUPX > 0)
                {
                    //copy the bytes
                    byte[] upxBytes = new byte[cbUPX];
                    Array.Copy(bytes, upxOffset + 2, upxBytes, 0, upxBytes.Length);

                    if (this.stk == StyleKind.table)
                    {
                        //first upx is TAPX; second PAPX, third CHPX
                        switch (i)
                        {
                        case 0:
                            this.tapx = new TablePropertyExceptions(upxBytes);
                            break;

                        case 1:
                            this.papx = new ParagraphPropertyExceptions(upxBytes, dataStream);
                            break;

                        case 2:
                            this.chpx = new CharacterPropertyExceptions(upxBytes);
                            break;
                        }
                    }
                    else if (this.stk == StyleKind.paragraph)
                    {
                        //first upx is PAPX, second CHPX
                        switch (i)
                        {
                        case 0:
                            this.papx = new ParagraphPropertyExceptions(upxBytes, dataStream);
                            break;

                        case 1:
                            this.chpx = new CharacterPropertyExceptions(upxBytes);
                            break;
                        }
                    }
                    else if (this.stk == StyleKind.list)
                    {
                        //list styles have only one PAPX
                        switch (i)
                        {
                        case 0: this.papx = new ParagraphPropertyExceptions(upxBytes, dataStream); break;
                        }
                    }
                    else if (this.stk == StyleKind.character)
                    {
                        //character styles have only one CHPX
                        switch (i)
                        {
                        case 0: this.chpx = new CharacterPropertyExceptions(upxBytes); break;
                        }
                    }
                }

                //increase the offset for the next run
                upxOffset += (2 + cbUPX);
            }
        }
Пример #4
0
 public Style WithStyleKind(StyleKind styleKind)
 => new Style(styleKind: styleKind, color: Color, background: Background, fontFlags: FontFlags);
Пример #5
0
        /// <summary>
        /// Perform a simple lexical styling of the given range of content.  This will *not*
        /// correctly style many constructs, and it's not smart enough to understand
        /// context.  But it's fast enough that it can style most content while you type, and
        /// it's accurate enough that it's good enough for a first pass.  Various background
        /// threads will be responsible for running the full parser and re-styling the content
        /// more accurately afterward.
        /// </summary>
        /// <param name="editor">The editor that holds the content.</param>
        /// <param name="startPos">The starting character position within the text to style.</param>
        /// <param name="endPos">The ending character position within the text to style.</param>
        /// <param name="filename">The name of the file being styled (for generating errors)</param>
        public static void StyleRange(Scintilla editor, TextEditorControl owner, int startPos, int endPos, string filename)
        {
            try
            {
                // Ensure startPos/endPos actually refer to the document.
                ValidateStartAndEndPos(editor, ref startPos, ref endPos);

                // Get the real range of lines to style.
                int startLine = editor.LineFromPosition(startPos);
                int endLine   = editor.LineFromPosition(endPos);

                // Remove all the error/warning marks in this range.
                owner.ClearLineMarks(startLine + 1, endLine - startLine + 1);

                // Now find the actual document start and end of the range we're going to color.
                int trueStartPos = editor.Lines[startLine].Position;
                int trueEndPos   = editor.Lines[endLine].EndPosition;

                // Get the text itself in that range.
                string textSlice = editor.GetTextRange(trueStartPos, trueEndPos - trueStartPos);

                // Ask the real Smile Lexer to begin lexical analysis on it.
                SmileLibInterop.Lexer lexer = new SmileLibInterop.Lexer(textSlice,
                                                                        0, textSlice.Length, filename, startLine + 1, 1, true);

                editor.StartStyling(trueStartPos);

                Token token;
                int   lastPos       = 0;
                Token previousToken = null;
                while ((token = lexer.Next()).Kind != TokenKind.EOI)
                {
                    LexerPosition lexerPosition = token.Position;
                    int           tokenStartPos = lexerPosition.LineStart + lexerPosition.Column - 1;
                    int           tokenLength   = lexerPosition.Length;

                    if (tokenStartPos > lastPos)
                    {
                        // Token somehow skipped over some content, so style it plain.
                        editor.SetStyling(tokenStartPos - lastPos, (int)StyleKind.Default);
                        editor.IndicatorClearRange(trueStartPos + lastPos, tokenStartPos - lastPos);

                        System.Diagnostics.Debug.WriteLine($"Warning: Lexer skipped {tokenStartPos - lastPos} characters at {lastPos}.");
                    }
                    else if (tokenStartPos < lastPos)
                    {
                        // For some reason, the previous token had too many characters in it,
                        // so shorten this one by a bit to make up for that mistake.
                        tokenLength -= lastPos - tokenStartPos;

                        System.Diagnostics.Debug.WriteLine($"Warning: Lexer grabbed too many characters ({lastPos - tokenStartPos} extra) at {lastPos}.");
                    }

                    if (tokenLength > 0)
                    {
                        StyleKind styleKind = GetDefaultStyleKindForToken(previousToken, token);
                        editor.SetStyling(tokenLength, (int)styleKind);

                        if (styleKind == StyleKind.Meta_Error)
                        {
                            editor.IndicatorCurrent = (int)IndicatorKind.Error;
                            editor.IndicatorFillRange(trueStartPos + tokenStartPos, tokenLength);
                            owner.SetLineMark(lexerPosition.Line, IndicatorKind.Error);
                        }
                        else
                        {
                            editor.IndicatorClearRange(trueStartPos + tokenStartPos, tokenLength);
                        }
                    }

                    lastPos       = tokenStartPos + tokenLength;
                    previousToken = IsSemanticToken(token) ? token : previousToken;
                }
            }
            catch (Exception e)
            {
                // Should never get here, but just in case, we swallow errors
                // and hope a later pass will restyle the content better.
                System.Diagnostics.Debug.WriteLine("Warning: LexicalStyler.StyleRange() crashed: " + e.Message);
            }
        }
Пример #6
0
        /// <summary>
        /// Perform a simple lexical styling of the given collection of lexical tokens.
        /// </summary>
        /// <param name="editor">The editor that holds the content.</param>
        /// <param name="tokens">The tokens to be restyled.</param>
        public static void StyleTokens(Scintilla editor, TextEditorControl owner, IEnumerable <Token> tokens)
        {
            try
            {
                bool  isFirst       = true;
                int   lastPos       = 0;
                int   trueStartPos  = 0;
                Token previousToken = null;
                foreach (Token token in tokens)
                {
                    LexerPosition lexerPosition = token.Position;
                    int           tokenStartPos = lexerPosition.LineStart + lexerPosition.Column - 1;
                    int           tokenEndPos   = tokenStartPos + lexerPosition.Length;
                    ValidateStartAndEndPos(editor, ref tokenStartPos, ref tokenEndPos);
                    int tokenLength = tokenEndPos - tokenStartPos;

                    if (isFirst)
                    {
                        editor.StartStyling(tokenStartPos);
                        trueStartPos = lastPos = tokenStartPos;
                        isFirst      = false;
                    }

                    if (tokenStartPos > lastPos)
                    {
                        // Token somehow skipped over some content, so style it plain.
                        editor.SetStyling(tokenStartPos - lastPos, (int)StyleKind.Default);
                        editor.IndicatorClearRange(trueStartPos + lastPos, tokenStartPos - lastPos);

                        System.Diagnostics.Debug.WriteLine($"Warning: Lexer skipped {tokenStartPos - lastPos} characters at {lastPos}.");
                    }
                    else if (tokenStartPos < lastPos)
                    {
                        // For some reason, the previous token had too many characters in it,
                        // so shorten this one by a bit to make up for that mistake.
                        tokenLength -= lastPos - tokenStartPos;

                        System.Diagnostics.Debug.WriteLine($"Warning: Lexer grabbed too many characters ({lastPos - tokenStartPos} extra) at {lastPos}.");
                    }

                    if (tokenLength > 0)
                    {
                        StyleKind styleKind = GetDefaultStyleKindForToken(previousToken, token);
                        editor.SetStyling(tokenLength, (int)styleKind);

                        if (styleKind == StyleKind.Meta_Error)
                        {
                            editor.IndicatorCurrent = (int)IndicatorKind.Error;
                            editor.IndicatorFillRange(trueStartPos + tokenStartPos, tokenLength);
                            owner.SetLineMark(lexerPosition.Line, IndicatorKind.Error);
                        }
                        else
                        {
                            editor.IndicatorClearRange(trueStartPos + tokenStartPos, tokenLength);
                        }
                    }

                    lastPos       = tokenStartPos + tokenLength;
                    previousToken = IsSemanticToken(token) ? token : previousToken;
                }
            }
            catch (Exception e)
            {
                // Should never get here, but just in case, we swallow errors
                // and hope a later pass will restyle the content better.
                System.Diagnostics.Debug.WriteLine("Warning: LexicalStyler.StyleTokens() crashed: " + e.Message);
            }
        }
Пример #7
0
 public StyleChangedEventArgs(StyleKind styleKind, Style?style)
 {
     StyleKind = styleKind;
     Style     = style;
 }
Пример #8
0
 public Style()
 {
     Kind = StyleKind.User;
     Layers.CollectionChanged += updateLayerStyle;
 }