PowerShell / PowerShell/PSScriptAnalyzer

When a line ends in an operator, automatically indent the next line since it is part of the same code statement

Đang mở
#2,003 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Area - Formatter Area-Code Formatting Issue-Enhancement
Ngôn ngữ chính
C#
Star
2.2k
Fork
414
Merge trung bình
13 giờ 1 phút
Pull request đã merge (30 ngày)
2

Mô tả

Summary of the new feature

As a user, when a line ends in a line continuation operator (=, +, -, etc.), I would like the next line to be indented so that it is clear both lines are part of the same statement, and that the 2nd line is not the start of a new code statement. This behaviour is already implemented for the pipe | operator, and it would be great if the other operators followed the same rules. This behaviour would also match the default behaviour of other languages, such as C# in Visual Studio. In VS Code, this behaviour is controlled for the pipe operator with the powershell.codeFormatting.pipelineIndentationStyle setting.

For example, when using the VS Code setting powershell.codeFormatting.pipelineIndentationStyle = IncreaseIndentationForFirstPipeline, the code is currently formatted like this:

$result =
Get-Process |
    Select-Object -First 1

I would prefer = to follow the same rules as | and have the code formatted like this:

$result =
    Get-Process |
    Select-Object -First 1

In this small example it may look silly to not just do:

$result = Get-Process |
    Select-Object -First 1

However, sometimes with fully qualified namespaces and descriptive variable names, the left-side of the equal operator may get very lengthy, such as:

[System.Collections.ArrayList] $listOfCustomersWithValidDriversLicenses = Get-CustomersFromCmdletWithAVeryLongName -Country Canada -AreaCode 123

This would look much better and prevent horizontal scrolling as:

[System.Collections.ArrayList] $listOfCustomersWithValidDriversLicenses = 
    Get-CustomersFromCmdletWithAVeryLongName -Country Canada -AreaCode 123

Currently, VS Code formats the code like this:

[System.Collections.ArrayList] $listOfCustomersWithValidDriversLicenses = 
Get-CustomersFromCmdletWithAVeryLongName -Country Canada -AreaCode 123

The current formatting makes it easy to overlook that the results returned from Get-CustomersFromCmdletWithAVeryLongName are being saved in a variable.

The example above is for the = operator, but I think it would make sense to do the same thing for +, -, and any other line continuation operators (i.e. that don't require a backtick ` to have the statement continue onto the next line).

Aside: I originally posted this issue in the vscode-powershell repo here, but I think this is the proper place for this feature request.

What is the latest version of PSScriptAnalyzer at the point of writing

The latest stable version of PSScriptAnalyzer is currently v1.22.0.

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách truy vết hành vi thụt lề hiện có của pipe | trong formatter của PSScriptAnalyzer và cách các toán tử tiếp tục dòng được phân loại. Mở rộng các quy tắc tương tự cho các toán tử như =, +-, sau đó kiểm tra đầu ra đã được định dạng so với các ví dụ và hành vi pipe hiện có.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
powershell
Lĩnh vực
tooling
Loại issue
Tính năng
Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức độ hoạt động
Đình trệ
Độ rõ ràng
Khá rõ ràng
Mức phù hợp với người mới
35/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.