-
Notifications
You must be signed in to change notification settings - Fork 236
(GH-793) Add syntax folding #777
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
392f855
bd76cdb
77aa878
94fdf89
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using Microsoft.PowerShell.EditorServices.Protocol.MessageProtocol; | ||
|
||
namespace Microsoft.PowerShell.EditorServices.Protocol.LanguageServer | ||
{ | ||
public class FoldingRangeRequest | ||
{ | ||
/// <summary> | ||
/// A request to provide folding ranges in a document. The request's | ||
/// parameter is of type [FoldingRangeParams](#FoldingRangeParams), the | ||
/// response is of type [FoldingRangeList](#FoldingRangeList) or a Thenable | ||
/// that resolves to such. | ||
/// Ref: https://github.com/Microsoft/vscode-languageserver-node/blob/5350bc2ffe8afb17357c1a66fbdd3845fa05adfd/protocol/src/protocol.foldingRange.ts#L112-L120 | ||
/// </summary> | ||
public static readonly | ||
RequestType<FoldingRangeParams, FoldingRange[], object, object> Type = | ||
RequestType<FoldingRangeParams, FoldingRange[], object, object>.Create("textDocument/foldingRange"); | ||
} | ||
|
||
/// <summary> | ||
/// Parameters for a [FoldingRangeRequest](#FoldingRangeRequest). | ||
/// Ref: https://github.com/Microsoft/vscode-languageserver-node/blob/5350bc2ffe8afb17357c1a66fbdd3845fa05adfd/protocol/src/protocol.foldingRange.ts#L102-L110 | ||
/// </summary> | ||
public class FoldingRangeParams | ||
{ | ||
/// <summary> | ||
/// The text document | ||
/// </summary> | ||
public TextDocumentIdentifier TextDocument { get; set; } | ||
} | ||
|
||
/// <summary> | ||
/// Represents a folding range. | ||
/// Ref: https://github.com/Microsoft/vscode-languageserver-node/blob/5350bc2ffe8afb17357c1a66fbdd3845fa05adfd/protocol/src/protocol.foldingRange.ts#L69-L100 | ||
/// </summary> | ||
public class FoldingRange | ||
{ | ||
/// <summary> | ||
/// The zero-based line number from where the folded range starts. | ||
/// </summary> | ||
public int StartLine { get; set; } | ||
|
||
/// <summary> | ||
/// The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line. | ||
/// </summary> | ||
public int StartCharacter { get; set; } | ||
|
||
/// <summary> | ||
/// The zero-based line number where the folded range ends. | ||
/// </summary> | ||
public int EndLine { get; set; } | ||
|
||
/// <summary> | ||
/// The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line. | ||
/// </summary> | ||
public int EndCharacter { get; set; } | ||
|
||
/// <summary> | ||
/// Describes the kind of the folding range such as `comment' or 'region'. The kind | ||
/// is used to categorize folding ranges and used by commands like 'Fold all comments'. See | ||
/// [FoldingRangeKind](#FoldingRangeKind) for an enumeration of standardized kinds. | ||
/// </summary> | ||
public string Kind { get; set; } | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,8 @@ public class ServerCapabilities | |
public ExecuteCommandOptions ExecuteCommandProvider { get; set; } | ||
|
||
public object Experimental { get; set; } | ||
|
||
public bool FoldingRangeProvider { get; set; } = false; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IMO, in this case, being explicit makes it more obvious what's going on. I don't feel there's any maintenance cost with leaving this in. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Works for me 👍 |
||
} | ||
|
||
/// <summary> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
// | ||
|
||
using System; | ||
|
||
namespace Microsoft.PowerShell.EditorServices | ||
{ | ||
/// <summary> | ||
/// A class that holds the information for a foldable region of text in a document | ||
/// </summary> | ||
public class FoldingReference: IComparable<FoldingReference> | ||
{ | ||
/// <summary> | ||
/// The zero-based line number from where the folded range starts. | ||
/// </summary> | ||
public int StartLine { get; set; } | ||
|
||
/// <summary> | ||
/// The zero-based character offset from where the folded range starts. If not defined, defaults to the length of the start line. | ||
/// </summary> | ||
public int StartCharacter { get; set; } = 0; | ||
|
||
/// <summary> | ||
/// The zero-based line number where the folded range ends. | ||
/// </summary> | ||
public int EndLine { get; set; } | ||
|
||
/// <summary> | ||
/// The zero-based character offset before the folded range ends. If not defined, defaults to the length of the end line. | ||
/// </summary> | ||
public int EndCharacter { get; set; } = 0; | ||
|
||
/// <summary> | ||
/// Describes the kind of the folding range such as `comment' or 'region'. | ||
/// </summary> | ||
public string Kind { get; set; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this based on the VSCode type? Would it be better as an enum? We could extend that as easily as a string, but it's faster to check, less error prone and can't be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Given that the VSCode API can have this as enum MatchKind
{
None = 0,
Comment,
Region
} (Or similar) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. See discussion above about strings and enums. This type is String (https://github.com/Microsoft/vscode-languageserver-node/blob/master/protocol/src/protocol.foldingRange.ts#L94-L99) With 3 (at this time) well known strings (https://github.com/Microsoft/vscode-languageserver-node/blob/master/protocol/src/protocol.foldingRange.ts#L51-L67) * Enum of known range kinds
*/
export enum FoldingRangeKind {
/**
* Folding range for a comment
*/
Comment = 'comment',
/**
* Folding range for a imports or includes
*/
Imports = 'imports',
/**
* Folding range for a region (e.g. `#region`)
*/
Region = 'region'
} I couldn't use an enum as C# enums are ints (well, ordinal) not strings. I later just used string constants as I only needed it in one place There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it because it's a JSON deserialisation thing? If so we should take a quick look at how we deserialise messages and see if we can use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In this case it's serialisation only. We don't receive this from a client. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I'm good with how we do this. I looked into restructuring it to allow enums, but given that it took several changes in various places, I might save it to go with some other work I've been wanting to do. |
||
|
||
/// <summary> | ||
/// A custom comparable method which can properly sort FoldingReference objects | ||
/// </summary> | ||
public int CompareTo(FoldingReference that) { | ||
// Initially look at the start line | ||
if (this.StartLine < that.StartLine) { return -1; } | ||
if (this.StartLine > that.StartLine) { return 1; } | ||
|
||
// They have the same start line so now consider the end line. | ||
// The biggest line range is sorted first | ||
if (this.EndLine > that.EndLine) { return -1; } | ||
if (this.EndLine < that.EndLine) { return 1; } | ||
|
||
// They have the same lines, but what about character offsets | ||
if (this.StartCharacter < that.StartCharacter) { return -1; } | ||
if (this.StartCharacter > that.StartCharacter) { return 1; } | ||
if (this.EndCharacter < that.EndCharacter) { return -1; } | ||
if (this.EndCharacter > that.EndCharacter) { return 1; } | ||
|
||
// They're the same range, but what about kind | ||
return string.Compare(this.Kind, that.Kind); | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.