0

I'm trying to color some specific part of the text, i have tried to say:

if word.strip().startswith(":"):
    self.setAttributesForRange(NSColor.greenColor(), None, highlightOffset, len(word))

When someone types the sign : it gets colored green. That is good, but it keeps coloring the word after it like this:

:Hello Hello :Hello <---- this all gets colored green, but I want something like:

:Hello Hello :Hello <---- where everything get colored except the middle "hello" because it doesn't start with the sign : , please help me out

from Foundation import *
from AppKit import *

import objc

class PyObjC_HighlightAppDelegate(NSObject):

    # The connection to our NSTextView in the UI
    highlightedText = objc.IBOutlet()

    # Default font size to use when highlighting
    fontSize = 12

    def applicationDidFinishLaunching_(self, sender):
        NSLog("Application did finish launching.")

    def textDidChange_(self, notification):




        """
        Delegate method called by the NSTextView whenever the contents of the
        text view have changed. This is called after the text has changed and
        been committed to the view. See the Cocoa reference documents:

        http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSText_Class/Reference/Reference.html
        http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSTextView_Class/Reference/Reference.html

        Specifically the sections on Delegate Methods for information on additional
        delegate methods relating to text control is NSTextView objects.
        """

        # Retrieve the current contents of the document and start highlighting

        content = self.highlightedText.string()
        self.highlightText(content)

    def setAttributesForRange(self, color, font, rangeStart, rangeLength):
        """
        Set the visual attributes for a range of characters in the NSTextView. If
        values for the color and font are None, defaults will be used.

        The rangeStart is an index into the contents of the NSTextView, and
        rangeLength is used in combination with this index to create an NSRange
        structure, which is passed to the NSTextView methods for setting
        text attributes. If either of these values are None, defaults will
        be provided.

        The "font" parameter is used as an key for the "fontMap", which contains
        the associated NSFont objects for each font style.
        """
        fontMap = {
                    "normal" : NSFont.systemFontOfSize_(self.fontSize),
                    "bold" : NSFont.boldSystemFontOfSize_(self.fontSize)
                    }

        # Setup sane defaults for the color, font and range if no values
        # are provided
        if color is None:
            color = NSColor.blackColor()
        if font is None:
            font = "normal"        

        if font not in fontMap:
            font = "normal"

        displayFont = fontMap[font]

        if rangeStart is None:
            rangeStart = 0

        if rangeLength is None:
            rangeLength = len(self.highlightedText.string()) - rangeStart

        # Set the attributes for the specified character range
        range = NSRange(rangeStart, rangeLength)
        self.highlightedText.setTextColor_range_(color, range)
        self.highlightedText.setFont_range_(displayFont, range)

    def highlightText(self, content):
        """
        Apply our customized highlighting to the provided content. It is assumed that
        this content was extracted from the NSTextView.
        """

        # Calling the setAttributesForRange with no values creates
        # a default that "resets" the formatting on all of the content
        self.setAttributesForRange(None, None, None, None)

        # We'll highlight the content by breaking it down into lines, and
        # processing each line one by one. By storing how many characters
        # have been processed we can maintain an "offset" into the overall
        # content that we use to specify the range of text that is currently
        # being highlighted.
        contentLines = content.split("\n")
        highlightOffset = 0

        for line in contentLines:

            if line.strip().startswith("#"):
                # Comment - we want to highlight the whole comment line
                self.setAttributesForRange(NSColor.greenColor(), None, highlightOffset, len(line))


            elif line.find(":") > -1:
                # Tag - we only want to highlight the tag, not the colon or the remainder of the line
                startOfLine = line[0: line.find(":")]
                yamlTag = startOfLine.strip("\t ")
                yamlTagStart = line.find(yamlTag)
                self.setAttributesForRange(NSColor.blueColor(), "bold", highlightOffset + yamlTagStart, len(yamlTag))

            elif line.strip().startswith("-"):
                # List item - we only want to highlight the dash
                listIndex = line.find("-")
                self.setAttributesForRange(NSColor.redColor(), None, highlightOffset + listIndex, 1)


            # Add the processed line to our offset, as well as the newline that terminated the line
            highlightOffset += len(line) + 1
BenMorel
  • 34,448
  • 50
  • 182
  • 322

1 Answers1

1

It all depends on what word is.

In [6]: word = ':Hello Hello :Hello'

In [7]: word.strip().startswith(':')
Out[7]: True

In [8]: len(word)
Out[8]: 19

Compare:

In [1]: line = ':Hello Hello :Hello'.split()

In [2]: line
Out[2]: [':Hello', 'Hello', ':Hello']

In [3]: for word in line:
    print word.strip().startswith(':')
    print len(word)
   ...:     
True
6
False
5
True
6

Notice the difference in len(word), which I suspect is causing your problem.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94