-
Notifications
You must be signed in to change notification settings - Fork 14
Add tools API and errors package #4
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6dcd433
Add tools API and errors package
snelusha 95ab461
Delegate `TextLines` to the `Lines()` method
snelusha 7d3fe1e
Refactor `IndexOutOfBoundsError` and `IllegalArgumentError`
snelusha d1d97b2
Refactor `calculateTextLines` to ignore rune (int32)
snelusha 8581550
Refactor `String()` to drop the empty‐slice guard
snelusha a5b6940
Return -1 instead of 0 on error
snelusha 8df6ffc
Use `any` type instead of `interface{}`
snelusha e3414b1
Add Copilot prompt file
snelusha 687b886
Use overflow-safe midpoint calc
snelusha cfb60de
Use `string` instead of `[]rune`
snelusha f997904
Address code review suggestions
snelusha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package errors | ||
|
|
||
| import "fmt" | ||
|
|
||
| type IndexOutOfBoundsError struct { | ||
| index int | ||
| length int | ||
| } | ||
|
|
||
| func (e IndexOutOfBoundsError) Error() string { | ||
| return fmt.Sprintf("Index %d out of bounds for length %d", e.index, e.length) | ||
| } | ||
|
|
||
| func (e IndexOutOfBoundsError) GetIndex() int { | ||
| return e.index | ||
| } | ||
|
|
||
| func (e IndexOutOfBoundsError) GetLength() int { | ||
| return e.length | ||
| } | ||
|
|
||
| func NewIndexOutOfBoundsError(index, length int) *IndexOutOfBoundsError { | ||
| return &IndexOutOfBoundsError{ | ||
| index: index, | ||
| length: length, | ||
| } | ||
| } | ||
|
|
||
| type IllegalArgumentError struct { | ||
| argument interface{} | ||
| } | ||
|
|
||
| func (e IllegalArgumentError) Error() string { | ||
| return fmt.Sprintf("Illegal argument: %v", e.argument) | ||
| } | ||
|
|
||
| func (e IllegalArgumentError) GetArgument() interface{} { | ||
| return e.argument | ||
| } | ||
|
|
||
| func NewIllegalArgumentError(argument interface{}) *IllegalArgumentError { | ||
| return &IllegalArgumentError{ | ||
| argument: argument, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package diagnostics | ||
|
|
||
| import ( | ||
| "ballerina-lang-go/tools/text" | ||
| "fmt" | ||
| ) | ||
|
|
||
| // DefaultDiagnostic is an internal implementation of the Diagnostic interface that is used by the DiagnosticFactory | ||
| // to create diagnostics. | ||
| type DefaultDiagnostic interface { | ||
| Diagnostic | ||
| } | ||
|
|
||
| type defaultDiagnosticImpl struct { | ||
| diagnosticBase | ||
| diagnosticInfo DiagnosticInfo | ||
| location Location | ||
| properties []DiagnosticProperty[interface{}] | ||
| message string | ||
| } | ||
|
|
||
| func NewDefaultDiagnostic(diagnosticInfo DiagnosticInfo, location Location, properties []DiagnosticProperty[any], args ...interface{}) DefaultDiagnostic { | ||
| message := formatMessage(diagnosticInfo.MessageFormat(), args...) | ||
| return &defaultDiagnosticImpl{ | ||
| diagnosticBase: diagnosticBase{}, | ||
| diagnosticInfo: diagnosticInfo, | ||
| location: location, | ||
| properties: properties, | ||
| message: message, | ||
| } | ||
snelusha marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (dd *defaultDiagnosticImpl) Location() Location { | ||
| return dd.location | ||
| } | ||
|
|
||
| func (dd *defaultDiagnosticImpl) DiagnosticInfo() DiagnosticInfo { | ||
| return dd.diagnosticInfo | ||
| } | ||
|
|
||
| func (dd *defaultDiagnosticImpl) Message() string { | ||
| return dd.message | ||
| } | ||
|
|
||
| func (dd *defaultDiagnosticImpl) Properties() []DiagnosticProperty[any] { | ||
| return dd.properties | ||
| } | ||
|
|
||
| func (dd *defaultDiagnosticImpl) String() string { | ||
| lineRange := dd.location.LineRange() | ||
| filePath := lineRange.FileName() | ||
|
|
||
| startLine := lineRange.StartLine() | ||
| endLine := lineRange.EndLine() | ||
|
|
||
| oneBasedStartLine := text.LinePositionFromLineAndOffset(startLine.Line()+1, startLine.Offset()+1) | ||
| oneBasedEndLine := text.LinePositionFromLineAndOffset(endLine.Line()+1, endLine.Offset()+1) | ||
| oneBasedLineRange := text.LineRangeFromLinePositions(filePath, oneBasedStartLine, oneBasedEndLine) | ||
|
|
||
| return fmt.Sprintf("%s [%s:%s] %s", | ||
| dd.diagnosticInfo.Severity().String(), | ||
| filePath, | ||
| oneBasedLineRange.String(), | ||
| dd.Message()) | ||
| } | ||
|
|
||
| func formatMessage(format string, args ...interface{}) string { | ||
| if len(args) == 0 { | ||
| return format | ||
| } | ||
| return fmt.Sprintf(format, args...) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package diagnostics | ||
|
|
||
| // DiagnosticCode represents a diagnostic code. | ||
| // Diagnostic code uniquely identifies a diagnostic. | ||
| type DiagnosticCode interface { | ||
| Severity() DiagnosticSeverity | ||
| DiagnosticId() string | ||
| MessageKey() string | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package diagnostics | ||
|
|
||
| // CreateDiagnostic creates a Diagnostic instance from the given details. | ||
| // | ||
| // Parameters: | ||
| // - diagnosticInfo: static diagnostic information | ||
| // - location: the location of the diagnostic | ||
| // - args: arguments to diagnostic message format | ||
| // | ||
| // Returns a Diagnostic instance. | ||
| func CreateDiagnostic(diagnosticInfo DiagnosticInfo, location Location, args ...interface{}) Diagnostic { | ||
snelusha marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return NewDefaultDiagnostic(diagnosticInfo, location, []DiagnosticProperty[any]{}, args...) | ||
snelusha marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // CreateDiagnosticWithProperties creates a Diagnostic instance from the given details. | ||
| // | ||
| // Parameters: | ||
| // - diagnosticInfo: static diagnostic information | ||
| // - location: the location of the diagnostic | ||
| // - properties: properties associated with the diagnostic | ||
| // - args: arguments to diagnostic message format | ||
| // | ||
| // Returns a Diagnostic instance. | ||
| func CreateDiagnosticWithProperties(diagnosticInfo DiagnosticInfo, location Location, properties []DiagnosticProperty[any], args ...interface{}) Diagnostic { | ||
| return NewDefaultDiagnostic(diagnosticInfo, location, properties, args...) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package diagnostics | ||
|
|
||
| // DiagnosticInfo represents an abstract shape of a Diagnostic that is independent of | ||
| // the location and message arguments. | ||
| type DiagnosticInfo interface { | ||
| Code() string | ||
| MessageFormat() string | ||
| Severity() DiagnosticSeverity | ||
| DiagnosticInfoLookupKey() DiagnosticInfoLookupKey | ||
| } | ||
|
|
||
| // DiagnosticInfoLookupKey represents the comparable fields of DiagnosticInfo for equality/hashing. | ||
| type DiagnosticInfoLookupKey struct { | ||
| Code *string // pointer to handle nil values | ||
| MessageFormat string | ||
| Severity DiagnosticSeverity | ||
| } | ||
|
|
||
| type diagnosticInfoImpl struct { | ||
| code *string // pointer to handle nil values | ||
| messageFormat string | ||
| severity DiagnosticSeverity | ||
| } | ||
|
|
||
| // NewDiagnosticInfo constructs an abstract shape of a Diagnostic. | ||
| // | ||
| // Parameters: | ||
| // - code: a code that can be used to uniquely identify a diagnostic category | ||
| // - messageFormat: a pattern that can be formatted with message formatting utilities | ||
| // - severity: the severity of the diagnostic | ||
| func NewDiagnosticInfo(code *string, messageFormat string, severity DiagnosticSeverity) DiagnosticInfo { | ||
| return &diagnosticInfoImpl{ | ||
| code: code, | ||
| messageFormat: messageFormat, | ||
| severity: severity, | ||
| } | ||
| } | ||
|
|
||
| func (di diagnosticInfoImpl) Code() string { | ||
| if di.code == nil { | ||
| return "" | ||
| } | ||
| return *di.code | ||
| } | ||
|
|
||
| func (di diagnosticInfoImpl) MessageFormat() string { | ||
| return di.messageFormat | ||
| } | ||
|
|
||
| func (di diagnosticInfoImpl) Severity() DiagnosticSeverity { | ||
| return di.severity | ||
| } | ||
|
|
||
| // DiagnosticInfoLookupKey returns the lookup key for equality comparisons. | ||
| func (di diagnosticInfoImpl) DiagnosticInfoLookupKey() DiagnosticInfoLookupKey { | ||
| return DiagnosticInfoLookupKey{ | ||
| Code: di.code, | ||
| MessageFormat: di.messageFormat, | ||
| Severity: di.severity, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package diagnostics | ||
|
|
||
| // DiagnosticPropertyKind represents the kind of the diagnostic property. | ||
| type DiagnosticPropertyKind uint8 | ||
|
|
||
| const ( | ||
| Symbolic DiagnosticPropertyKind = iota | ||
| String | ||
| Numeric | ||
| Collection | ||
| Other | ||
| ) | ||
|
|
||
| // String returns the string representation of the diagnostic property kind. | ||
| func (dpk DiagnosticPropertyKind) String() string { | ||
| switch dpk { | ||
| case Symbolic: | ||
| return "SYMBOLIC" | ||
| case String: | ||
| return "STRING" | ||
| case Numeric: | ||
| return "NUMERIC" | ||
| case Collection: | ||
| return "COLLECTION" | ||
| case Other: | ||
| return "OTHER" | ||
| default: | ||
| return "UNKNOWN" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* | ||
| * Copyright (c) 2025, WSO2 LLC. (http://www.wso2.com). | ||
| * | ||
| * WSO2 LLC. licenses this file to you under the Apache License, | ||
| * Version 2.0 (the "License"); you may not use this file except | ||
| * in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
|
|
||
| package diagnostics | ||
|
|
||
| // DiagnosticProperty represents properties passed when diagnostic logging. | ||
| type DiagnosticProperty[T interface{}] interface { | ||
| Kind() DiagnosticPropertyKind | ||
| Value() T | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.