Xcode: code loses syntax coloring
Asked Answered
E

15

80

I find that in various situations Objective-C code in Xcode 3.1 (Leopard) can fail to get appropriate syntax coloring after typing or lose coloring that it had.

This isn't just a "refresh" issue with new custom symbols -- but affects Cocoa framework symbols as well.

Sometimes CMD-a to select all text on the code page will make the coloring (re)appear, sometimes double-clicking on a line to select it will work, sometimes I have to add/delete a space in a symbol to get that symbol to (re)color. Rebuilding, or closing/reopening the project may or may not work.

Is this a known issue with Xcode? For something so annoying to me, I'm not finding the plentiful discussions of it on SO and elsewhere I'd expect.

And is there any command to force global syntax recoloring?

Enucleate answered 26/1, 2010 at 7:38 Comment(1)
Also check my answer here #1627533Strain
M
148

1.) Go to Project --> Build Settings --> Header Search Paths

2.) Add "$(SRCROOT)/**"

3.) Close and reload the project

4.) Go to Xcode --> Window --> Organizer --> Projects --> $YourProject --> Delete Derived Data

5.) Wait a moment until XCode rebuild all indicies


To get more information activate verbose logging:

1.) Increase Xcode log level in Terminal app: defaults write com.apple.dt.Xcode IDEIndexingClangInvocationLogLevel 3

2.) Open Console app. Search for "xcode"

3.) I saw "file not found" errors for header files mentioned in the pch file (therefore the fix above)

Malacostracan answered 6/10, 2011 at 15:16 Comment(6)
the second part of your answer was very helpful!!Richly
My experience in the labs at WWDC this year was if you want syntax highlighting to work, turn off pre-compilation completely... it worked for me but seems like an inadequate fix thoughDarindaring
Thanks a lot! #4 (deleting derived data) worked for me as well.Trisyllable
Simple yet efficient. Wouldn't "Clean" do the same as "Delete Derived data"?Lavoie
Gosh! You saved my life! I had been dealing with all this white code past few days and deleting DerivedData didn't help AFAIR. #2 worked for me. Thanks.Gorged
This worked for me in Xcode 8.3, while another solution that previously worked under 8.2 ceased to restore syntax highlighting. Great, timeless answer!Blus
S
43

I had the same issue with a project that was fine in Xcode 3 and for which the syntax highlighting and code completion has broken in Xcode 4. It took me the better half of the day to figure it out but I found that the following steps will reproduce / fix the issue 100% for me.

After trying all the aforementioned solutions to no avail, I tried creating the project from scratch and reimporting every file and folder painstakingly. Every time I imported a couple of files I checked and syntax highlighting still worked. Even after the last few of them it still did. Then I hit build which failed because I forgot to include a couple of common header files in the project .pch file that were there in the old project. And that was when CodeSense broke again.

After further investigation we found that if you import a header file that in turn imports the same framework header file than the .pch already does, then CodeSense will break. The same setup did not cause any issues in Xcode 3.

Example:

project.pch

#import <Foundation/Foundation.h>
#import "projectConfig.h"

projectConfig.h

#import <Foundation/Foundation.h>
#import "one.h"
#import "two.h"

So by removing the wholly unnecessary

#import <Foundation/Foundation.h>

directive from the 'projectConfig.h' you can fix the highlighting and code completion will come back for you as well. If your issue is the same as mine that is.

Showoff answered 17/5, 2011 at 15:13 Comment(8)
yes ... just had the same issue ... and rage's solution fixed it.Digitoxin
Why isn't this up-voted into the stratosphere? I didn't bother cleaning up all of my header issues and instead just turned off the Precompile Prefix Header option for my target and all is well. Finally I can have the entire team upgrade to 4.2 since that is where we broke.Lamplighter
It works! I did the same as @Eggett Booth, i turned off "Precompile Prefix Header" (set it to NO), cleaned my project and deleted its derived data. My syntax highlight restarted to work!Indopacific
this fixed my issue. Mine wasn't the Foundation file but a custom class. I just removed it from the .pch file then readded it and removed all instances of it in my other class files.Ureide
You have my thanks. :) I followed your description exactly. Removed Foundation and UIKit from all files that I imported in my .PCH.Pagepageant
This helped me a lot. I also found a problem with some headers that were not being included in Prefix.pch. To find which ones I used the command ´defaults write com.apple.dt.Xcode IDEIndexingClangInvocationLogLevel 3´ to increase de log level in Xcode. In Console.app I started seeing missing .h errors in Prefix.pch that were caused by Xcode not being able to find the headers in a private framework. I explicitly added the header search path in my project.Exudation
I used the global Find panel to locate all instances where <Foundation> was being imported and commented all out (except of course .pch). That didn't immediately bring back coloring, so I did an Analyze (more just as a way to make sure I hadn't messed something up and a cheap way to save all the changed documents). That didn't bring back coloring, so I closed the workspace and deleted derived data. On next open coloring was back. Prior to this occasion, relaunching Xcode had worked, so thanks for the continued investigation everyone! (And I filed a bug... ;-)Katharynkathe
Great answer. I had to delete files in organiser first to "fix" syntax highlighting once, but then it stopped breaking again.Denis
W
16

Just select the file who lost the syntax coloring , press Editor > Syntax Coloring > ( choose your language (Objective c++ for ex) instead of 'Default'

Wallop answered 16/4, 2011 at 18:9 Comment(2)
This happened to me. Not sure why XCode would default to Generic instead of right file type just for one file, but this step solved it. I've tried clean and deleting derived data first, which I assumed was a culprit.Telemetry
cheers... apple was selecting AppleScript for me lol thanks mateOrange
K
7

I have found the best way to regain syntax coloring is just to quit Xcode and re-launch it. I couldn't tell you why, but that works every time.

Kuo answered 7/8, 2011 at 1:54 Comment(2)
For me it even sufficed to re-open the project.Highjack
How Crazy is that! Not only did it fix the highlighting the category which previously was failing also started workingIntervocalic
O
4

I find that the following will often do the trick…

"Reselect" both "Base SDK" - and "Architecture" - in "Build Settings". (CLick on blue project icon in top left, while in "Browser mode", aka 1, then click project settings and mess with the stuff there.

Make sure they all line up / it doesn't hurt to do a little toggly-wogglying up in there, while you're in there… ya know just mix it up a bit. You'll notice xCode has a teensy-tiny mini-stroke - every time you switch between 64 and 32 bit… This is usually when I know if code highlighting is dead for the night, or if I'm good to go.

Often, I will have a project that gets highlighted instantly in 32 bit mode, and just sits there in 64… Who knows.. Ahh, Xcode, you wacky botch.

Override answered 4/4, 2012 at 10:31 Comment(2)
The answer helped me a lot and worked... But your writing style could be improved :)Scimitar
In retrospect, yes, this answer may indeed be incomprehensible to any non-native English speaker. Alas, to me... it's hillairballs.Override
E
3

Select your Project -> Build Settings tab -> Apple LLVM Language 5.0 -> Precompile Prefix Header to YES. NSLog was not highlighting. Currently running Xcode 5.0.1 Hope it helps =) Thanks @rebelzach

Eggett answered 13/3, 2014 at 5:33 Comment(0)
O
2

I can confirm a 100% fix for this problem, based on a question I had asked (and actually answered), as pictured below. One thing i will add is that sometimes it is "errant whitespace" that can also contribute to the loss of highlighting. Either clean it up yourself, or use the Goggle Toolbox for Mac Xcode 4 plugin, which adds an entry in the XCode Edit menu called Clean Up Whitespace.

enter image description here enter image description here

Override answered 14/4, 2012 at 19:18 Comment(0)
L
2

This appears to be a similar error from this question

I was able to correct this issue by setting Precompile Prefix Header to No in the build settings.

Lattonia answered 24/1, 2013 at 18:23 Comment(0)
D
1

I recently had a similar issue, all coloring and autocomplete stopped working for built-in frameworks, my own classes still worked. Someone recommended to me that I turn off "Run Static Analyzer" in the build settings. It's weird, but it worked.

Deafanddumb answered 5/5, 2010 at 2:0 Comment(0)
N
1

For me it was that I renamed my project, and Tests target was still pointing to the old pch location, but my App target was pointing to the new one.

If your app delegate still has code sense, this might be the case for you too. Go into project, select the 'Tests' target search 'prefix' and set it to the equivalent value in the 'app' target.

Narcolepsy answered 11/1, 2013 at 17:43 Comment(0)
I
0

Open the project settings (Project > Edit Project Settings menu) and then click the Rebuild Code Sense Index button in the General tab. This will recreate the index that Xcode uses for syntax coloring.

This process can take a while for larger projects, so you can check the progress in the Activity window (Window > Activity menu).

Invasive answered 28/1, 2010 at 5:51 Comment(2)
Time after time this doesn't fix the problem for me. I must be doing something wrong, as XCode simply doesn't bother highlighting syntax or displaying the auto completion.Livesay
Please make sure that you log bugs against Apple about these kinds of problems: bugreport.apple.com.Misshapen
C
0

Moreover, in XCode4 there is no button to rebuild the code sense index.

Caseworm answered 24/3, 2011 at 21:9 Comment(2)
I read somewhere that this is probably because XCode4 no longer needs a code sense index. I think it uses the new Clang syntax analysis directly instead of keeping a separate index. This is also why the code suggestions are much more accurate since it only suggests code completions that make sense from a compiler standpoint.Kali
It turns out there is a way - #1627533Cristiecristin
E
0

I had the same problem and solved this by updating my build settings to use the system default compiler, LLVM GCC 4.2.

It seems there are multiple factors.

Eam answered 28/4, 2011 at 13:22 Comment(0)
O
0

I experience this issue as well, and have tried the solutions either "rebuilding the index", "relaunch XCode", "change the compiler setting to LLVM GCC 4.2". Those didn't help me out.

So I tried to add the following code right away along with the fresh creation of header file.

#include "header.h"

using namespace cocos2d

Then, the code added earlier in header file is colorized, and it can recognize classes and stuff.

I've used cocos2d-x, so my syntax is based on c++. I don't test it with Objective-c yet though.

Odelsting answered 18/12, 2011 at 1:20 Comment(0)
A
0

For someone still have don't know why. Let open with XCode 8.2, it should work

Abolition answered 6/6, 2017 at 4:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.