Browse Source

SD2-759. Xml completion window no longer displayed when typing in space characters inside the attribute value.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.0@1304 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Matt Ward 20 years ago
parent
commit
f457d2ed4b
  1. 8
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlCompletionDataProvider.cs
  2. 76
      src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs
  3. 102
      src/AddIns/DisplayBindings/XmlEditor/Test/Parser/InsideAttributeValueTestFixture.cs
  4. 1
      src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj

8
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlCompletionDataProvider.cs

@ -63,9 +63,11 @@ namespace ICSharpCode.XmlEditor @@ -63,9 +63,11 @@ namespace ICSharpCode.XmlEditor
case ' ':
// Attribute intellisense.
XmlElementPath path = XmlParser.GetActiveElementStartPath(text, text.Length);
if (path.Elements.Count > 0) {
return GetAttributeCompletionData(path);
if (!XmlParser.IsInsideAttributeValue(text, text.Length)) {
XmlElementPath path = XmlParser.GetActiveElementStartPath(text, text.Length);
if (path.Elements.Count > 0) {
return GetAttributeCompletionData(path);
}
}
break;

76
src/AddIns/DisplayBindings/XmlEditor/Project/Src/XmlParser.cs

@ -145,9 +145,7 @@ namespace ICSharpCode.XmlEditor @@ -145,9 +145,7 @@ namespace ICSharpCode.XmlEditor
return false;
}
if (index >= xml.Length) {
index = xml.Length - 1;
}
index = GetCorrectedIndex(xml.Length, index);
// Move back one character if the last character is an '='
if (xml[index] == '=') {
@ -202,9 +200,7 @@ namespace ICSharpCode.XmlEditor @@ -202,9 +200,7 @@ namespace ICSharpCode.XmlEditor
return String.Empty;
}
if (index >= xml.Length) {
index = xml.Length - 1;
}
index = GetCorrectedIndex(xml.Length, index);
string name = String.Empty;
@ -299,6 +295,58 @@ namespace ICSharpCode.XmlEditor @@ -299,6 +295,58 @@ namespace ICSharpCode.XmlEditor
return false;
}
/// <summary>
/// Determines whether the specified index is inside an attribute value.
/// </summary>
public static bool IsInsideAttributeValue(string xml, int index)
{
if (xml.Length == 0) {
return false;
}
index = GetCorrectedIndex(xml.Length, index);
int elementStartIndex = GetActiveElementStartIndex(xml, index);
if (elementStartIndex == - 1) {
return false;
}
// Count the number of double quotes and single quotes that exist
// before the first equals sign encountered going backwards to
// the start of the active element.
bool foundEqualsSign = false;
int doubleQuotesCount = 0;
int singleQuotesCount = 0;
char lastQuoteChar = ' ';
for (int i = index; i > elementStartIndex; --i) {
char ch = xml[i];
if (ch == '=') {
foundEqualsSign = true;
break;
} else if (ch == '\"') {
lastQuoteChar = ch;
++doubleQuotesCount;
} else if (ch == '\'') {
lastQuoteChar = ch;
++singleQuotesCount;
}
}
bool isInside = false;
if (foundEqualsSign) {
// Odd number of quotes?
if ((lastQuoteChar == '\"') && ((doubleQuotesCount % 2) > 0)) {
isInside = true;
} else if ((lastQuoteChar == '\'') && ((singleQuotesCount %2) > 0)) {
isInside = true;
}
}
return isInside;
}
/// <summary>
/// Gets the text of the xml element start tag that the index is
/// currently inside.
@ -444,5 +492,21 @@ namespace ICSharpCode.XmlEditor @@ -444,5 +492,21 @@ namespace ICSharpCode.XmlEditor
return reversedString.ToString();
}
/// <summary>
/// Ensures that the index is on the last character if it is
/// too large.
/// </summary>
/// <param name="length">The length of the string.</param>
/// <param name="index">The current index.</param>
/// <returns>The index unchanged if the index is smaller than the
/// length of the string; otherwise it returns length - 1.</returns>
static int GetCorrectedIndex(int length, int index)
{
if (index >= length) {
index = length - 1;
}
return index;
}
}
}

102
src/AddIns/DisplayBindings/XmlEditor/Test/Parser/InsideAttributeValueTestFixture.cs

@ -0,0 +1,102 @@ @@ -0,0 +1,102 @@
// <file>
// <copyright see="prj:///doc/copyright.txt"/>
// <license see="prj:///doc/license.txt"/>
// <owner name="Matthew Ward" email="mrward@users.sourceforge.net"/>
// <version>$Revision$</version>
// </file>
using ICSharpCode.XmlEditor;
using NUnit.Framework;
using System;
using System.Xml;
namespace XmlEditor.Tests.Parser
{
[TestFixture]
public class InsideAttributeValueTestFixture
{
[Test]
public void InvalidString()
{
Assert.IsFalse(XmlParser.IsInsideAttributeValue(String.Empty, 10));
}
[Test]
public void DoubleQuotesTest1()
{
string xml = "<foo a=\"";
Assert.IsTrue(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void DoubleQuotesTest2()
{
string xml = "<foo a=\"\" ";
Assert.IsFalse(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void DoubleQuotesTest3()
{
string xml = "<foo a=\"\"";
Assert.IsFalse(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void DoubleQuotesTest4()
{
string xml = "<foo a=\" ";
Assert.IsTrue(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void NoXmlElementStart()
{
string xml = "foo a=\"";
Assert.IsFalse(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void EqualsSignTest()
{
string xml = "<foo a=";
Assert.IsFalse(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void SingleQuoteTest1()
{
string xml = "<foo a='";
Assert.IsTrue(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void MixedQuotesTest1()
{
string xml = "<foo a='\"";
Assert.IsTrue(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void MixedQuotesTest2()
{
string xml = "<foo a=\"'";
Assert.IsTrue(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void MixedQuotesTest3()
{
string xml = "<foo a=\"''";
Assert.IsTrue(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
[Test]
public void MixedQuotesTest4()
{
string xml = "<foo a=\"''\"";
Assert.IsFalse(XmlParser.IsInsideAttributeValue(xml, xml.Length));
}
}
}

1
src/AddIns/DisplayBindings/XmlEditor/Test/XmlEditor.Tests.csproj

@ -91,6 +91,7 @@ @@ -91,6 +91,7 @@
<Compile Include="Utils\SchemaIncludeTestFixtureHelper.cs" />
<Compile Include="Schema\MissingSchemaElementTestFixture.cs" />
<Compile Include="Schema\AllElementTestFixture.cs" />
<Compile Include="Parser\InsideAttributeValueTestFixture.cs" />
</ItemGroup>
<ItemGroup>
<Folder Include="Schema\" />

Loading…
Cancel
Save