Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / editor

Workarounds for Code Project's Text Editor

5.00/5 (5 votes)
24 Sep 2016CPOL2 min read 20.7K  
Although the text editor from the site is very powerful, sometimes... well if you use it, you know what I mean ;)

Having trouble writing or posting articles? These articles aim to gather together tips and tricks from authors and mentors to help you write great articles.

Introduction

Code Project (CP) is one of my favourite sites in the net. It is amazing how the team works under pressure speed solving problems or includes new features based on suggestions. But they are still humans, what means, they can't solve everything at once.

That's why some annoying things are still there, troubling the daily routine of the site.

Background

One source for tiny (or not so tiny) headaches is the article editor. There already are some very nice explanations about the editor and site formatting here in CP. Some I find good are:


But I keep seeing articles / tips / Q&A items having troubles with the code snippets.

For my explanation, I am using SQL code snippets extracted from Use SQL Server to Manage Completely DB Backups on Mirrored Databases. I have chosen this article, because it has had both the problems I am going to explain. If the article still is live, you can have a look to the versions history.

Issue 1: Copy and Paste Code from an Already Formatted Source

I would reckon that the author copied his code in his SQL environment (which is already formatted), when pasting here the colors of SQL environment were codified to HTML with the result...

<font color="#0000ff" face="Consolas" size="2"> 
<font color="#0000ff" face="Consolas" size="2"> 
<font color="#0000ff" face="Consolas" size="2"> 
EXEC</font></font></font>

How to insert code correctly is covered in OriginalGriff's tip in a professional way, but there is another possibility to avoid this using a simple workaround... paste it first in a pure plain text editor and then copy again. For Windows users, the best would be to use the Notepad.
In other words, copy in SQL and paste in Notepad, then copy in Notepad and paste in CP-Editor.

Doing this and choosing the correct language (Anshul's tip) in your code format (for this example "SQL") will give the result:

SQL
EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure'xp_cmdshell', 1
RECONFIGURE
GO
-- Show
EXEC sp_configure 'xp_cmdshell'

Issue 2: Escaped Quotes Mess the Format Coloring Everything as String

As we all know, sometimes when working with string / text, we have to escape some special symbols like [ \, ", ', : ] using another backslash before the special symbol [ \\, \", \', \: ]. This seems to drive the editor crazy and fool it with the result that it thinks everything afterwards is a string and formats it using the color purple.

Getting back to the previous SQL article as an example...

SQL
iF @Folder_Path LIKE '%' + '\'
SET @Folder_Path = substring(@Folder_Path, 1, len(@Folder_Path) - len('\'))
 
--Create a temp table to hold the results.
 
IF OBJECT_ID('tempdb..#DirectoryTree') IS NOT NULL
DROP TABLE #DirectoryTree;
 
CREATE TABLE #DirectoryTree (
id INT IDENTITY(1,1)
,fullpath VARCHAR(2000)
,subdirectory NVARCHAR(512)
,depth INT
,isfile BIT);

or using another format, like C#:

C#
int Pos = file.LastIndexOf("\\");
if (Pos != null)
{
   // doing things here
}

// some other stuff here

Path = Folder_Path + '\' + subdirectory
if (Path == null)
{ 
   return;
}

// do other things

This can be simply solved by adding a comment with the same special symbol at the end of the line producing the chaos. Please note that the comment symbol must be appropriate for the programming language.

Using this workaround would give us the result:

SQL
iF @Folder_Path LIKE '%' + '\'  --'
SET @Folder_Path = substring(@Folder_Path, 1, len(@Folder_Path) - len('\')) --'
 
--Create a temp table to hold the results.
 
IF OBJECT_ID('tempdb..#DirectoryTree') IS NOT NULL
DROP TABLE #DirectoryTree;
 
CREATE TABLE #DirectoryTree (
id INT IDENTITY(1,1)
,fullpath VARCHAR(2000)
,subdirectory NVARCHAR(512)
,depth INT
,isfile BIT);

and...

C#
int Pos = file.LastIndexOf("\\");  //"
if (Pos != null)
{
   // doing things here
}

// some other stuff here

Path = Folder_Path + '\' + subdirectory  //'
if (Path == null)
{ 
   return;
}

// do other things

I know that this will become obsolete at some time because CP will have solved the bug, but in the meanwhile... I hope it helps you.

History

  • 4th September, 2016 - v1.0 - Initial creation

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)