Skip to content

Commit 8268d23

Browse files
authored
Merge branch 'main' into dklilley-bugfix-PackageOnMac
2 parents 1a0f326 + 725bd77 commit 8268d23

File tree

6 files changed

+128
-0
lines changed

6 files changed

+128
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ MATLAB language server supports these editors by installing the corresponding ex
2727

2828
Fixed:
2929
* Resolved packaging failure on Mac
30+
* General bug fixes
3031

3132
### 1.2.1
3233
Release date: 2024-04-04
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
classdef (Hidden) FoldingSupportHandler < matlabls.handlers.FeatureHandler
2+
% FOLDINGSUPPORTHANDLER The feature handler for retrieving a document's
3+
% folding ranges.
4+
5+
% Copyright 2024 The MathWorks, Inc.
6+
7+
8+
properties (Access = private)
9+
RequestChannel = "/matlabls/foldDocument/request"
10+
ResponseChannel = "/matlabls/foldDocument/response"
11+
end
12+
13+
methods
14+
function this = FoldingSupportHandler ()
15+
this = this@matlabls.handlers.FeatureHandler();
16+
this.RequestSubscriptions = matlabls.internal.CommunicationManager.subscribe(this.RequestChannel, @this.handleFoldingRangeRequest);
17+
end
18+
end
19+
20+
methods (Access = private)
21+
function handleFoldingRangeRequest (this, msg)
22+
% Handles folding range requests
23+
codeToFold = msg.code;
24+
25+
fRangesArray = matlabls.internal.getFoldingRanges(codeToFold);
26+
response.data = fRangesArray;
27+
28+
% Send folding ranges
29+
responseChannel = strcat(this.ResponseChannel, '/', msg.channelId);
30+
matlabls.internal.CommunicationManager.publish(responseChannel, response.data)
31+
end
32+
end
33+
end
341 Bytes
Binary file not shown.

matlab/+matlabls/MatlabLanguageServerHelper.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ function initializeFeatureHandlers (this)
3131
this.FeatureHandlers(end + 1) = matlabls.handlers.IndexingHandler();
3232
this.FeatureHandlers(end + 1) = matlabls.handlers.LintingSupportHandler();
3333
this.FeatureHandlers(end + 1) = matlabls.handlers.NavigationSupportHandler();
34+
this.FeatureHandlers(end + 1) = matlabls.handlers.FoldingSupportHandler();
3435
end
3536
end
3637
end
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2024 The MathWorks, Inc.
2+
3+
import { FoldingRangeParams, TextDocuments, FoldingRange} from 'vscode-languageserver'
4+
import { TextDocument } from 'vscode-languageserver-textdocument'
5+
import { URI } from 'vscode-uri'
6+
import MatlabLifecycleManager from '../../lifecycle/MatlabLifecycleManager'
7+
import { MatlabConnection } from '../../lifecycle/MatlabCommunicationManager'
8+
9+
10+
class FoldingSupportProvider {
11+
private readonly REQUEST_CHANNEL = '/matlabls/foldDocument/request'
12+
private readonly RESPONSE_CHANNEL = '/matlabls/foldDocument/response'
13+
14+
async handleFoldingRangeRequest (params: FoldingRangeParams, documentManager: TextDocuments<TextDocument>): Promise<FoldingRange[] | null> {
15+
const docToFold = documentManager.get(params.textDocument.uri)
16+
if (docToFold == null) {
17+
return null
18+
}
19+
20+
const matlabConnection = await MatlabLifecycleManager.getMatlabConnection()
21+
const isMatlabAvailable = (matlabConnection != null)
22+
const matlabRelease = MatlabLifecycleManager.getMatlabRelease()
23+
24+
// check for connection and release
25+
if (!isMatlabAvailable || (matlabRelease == null) || (matlabRelease < 'R2024b')) {
26+
return null
27+
}
28+
29+
const fileName = URI.parse(docToFold.uri).fsPath
30+
const code = docToFold.getText()
31+
32+
const frArray = await this.getFoldingRangesFromMatlab(code, fileName, matlabConnection)
33+
34+
const foldingRanges = this.processFoldingRanges(frArray)
35+
36+
return foldingRanges;
37+
}
38+
39+
/**
40+
* Gets folding ranges from MATLAB.
41+
*
42+
* @param code The code in the file
43+
* @param fileName The file's name
44+
* @param matlabConnection The connection to MATLAB
45+
* @returns An array of line numbers
46+
*/
47+
private async getFoldingRangesFromMatlab (code: string, fileName: string, matlabConnection: MatlabConnection): Promise<number[]> {
48+
return await new Promise<number[]>(resolve => {
49+
const channelId = matlabConnection.getChannelId()
50+
const channel = `${this.RESPONSE_CHANNEL}/${channelId}`
51+
const responseSub = matlabConnection.subscribe(channel, message => {
52+
matlabConnection.unsubscribe(responseSub)
53+
resolve(message as number[])
54+
})
55+
56+
matlabConnection.publish(this.REQUEST_CHANNEL, {
57+
code,
58+
fileName,
59+
channelId
60+
})
61+
})
62+
}
63+
64+
/**
65+
* Processes folding range data from MATLAB.
66+
*
67+
* @param frArray An array of line numbers from MATLAB
68+
* @returns An array of FoldingRanges
69+
*/
70+
private processFoldingRanges (frArray: number[]): FoldingRange[] {
71+
let fRangeArray: FoldingRange[] = []
72+
73+
for(let i = 0; i < frArray.length; i = i+2) {
74+
let fRange = FoldingRange.create(frArray[i] - 1, frArray[i+1] - 1)
75+
fRangeArray.push(fRange)
76+
}
77+
78+
return fRangeArray
79+
}
80+
}
81+
82+
export default new FoldingSupportProvider()

src/server.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import ExecuteCommandProvider, { MatlabLSCommands } from './providers/lspCommand
1616
import NavigationSupportProvider, { RequestType } from './providers/navigation/NavigationSupportProvider'
1717
import LifecycleNotificationHelper from './lifecycle/LifecycleNotificationHelper'
1818
import MVM from './mvm/MVM'
19+
import FoldingSupportProvider from './providers/folding/FoldingSupportProvider'
20+
import { FoldingRange } from 'vscode-languageserver'
1921

2022
// Create a connection for the server
2123
export const connection = createConnection(ProposedFeatures.all)
@@ -68,6 +70,7 @@ connection.onInitialize((params: InitializeParams) => {
6870
executeCommandProvider: {
6971
commands: Object.values(MatlabLSCommands)
7072
},
73+
foldingRangeProvider: true,
7174
referencesProvider: true,
7275
signatureHelpProvider: {
7376
triggerCharacters: ['(', ',']
@@ -178,6 +181,14 @@ connection.onSignatureHelp(async params => {
178181
return await CompletionProvider.handleSignatureHelpRequest(params, documentManager)
179182
})
180183

184+
/** -------------------- FOLDING SUPPORT -------------------- **/
185+
connection.onFoldingRanges(async params => {
186+
// Retrieve the folding ranges
187+
// If there are valid folding ranges, hand them back to the IDE
188+
// Else, return null, so the IDE falls back to indent-based folding
189+
return await FoldingSupportProvider.handleFoldingRangeRequest(params, documentManager)
190+
})
191+
181192
/** -------------------- FORMATTING SUPPORT -------------------- **/
182193
connection.onDocumentFormatting(async params => {
183194
// Gather a set of document edits required for formatting, which the IDE will execute

0 commit comments

Comments
 (0)