- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 512
feat: expose Arduino state to VS Code extensions #2110
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 all commits
      Commits
    
    
            Show all changes
          
          
            3 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
          
            179 changes: 179 additions & 0 deletions
          
          179 
        
  arduino-ide-extension/src/browser/contributions/update-arduino-state.ts
  
  
      
      
   
        
      
      
    
  
    
      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,179 @@ | ||
| import { DisposableCollection } from '@theia/core/lib/common/disposable'; | ||
| import URI from '@theia/core/lib/common/uri'; | ||
| import { inject, injectable } from '@theia/core/shared/inversify'; | ||
| import { HostedPluginSupport } from '@theia/plugin-ext/lib/hosted/browser/hosted-plugin'; | ||
| import type { ArduinoState } from 'vscode-arduino-api'; | ||
| import { | ||
| BoardsService, | ||
| CompileSummary, | ||
| Port, | ||
| isCompileSummary, | ||
| } from '../../common/protocol'; | ||
| import { | ||
| toApiBoardDetails, | ||
| toApiCompileSummary, | ||
| toApiPort, | ||
| } from '../../common/protocol/arduino-context-mapper'; | ||
| import type { BoardsConfig } from '../boards/boards-config'; | ||
| import { BoardsDataStore } from '../boards/boards-data-store'; | ||
| import { BoardsServiceProvider } from '../boards/boards-service-provider'; | ||
| import { CurrentSketch } from '../sketches-service-client-impl'; | ||
| import { SketchContribution } from './contribution'; | ||
|  | ||
| interface UpdateStateParams<T extends ArduinoState> { | ||
| readonly key: keyof T; | ||
| readonly value: T[keyof T]; | ||
| } | ||
|  | ||
| /** | ||
| * Contribution for updating the Arduino state, such as the FQBN, selected port, and sketch path changes via commands, so other VS Code extensions can access it. | ||
| * See [`vscode-arduino-api`](https://github.com/dankeboy36/vscode-arduino-api#api) for more details. | ||
| */ | ||
| @injectable() | ||
| export class UpdateArduinoState extends SketchContribution { | ||
| @inject(BoardsService) | ||
| private readonly boardsService: BoardsService; | ||
| @inject(BoardsServiceProvider) | ||
| private readonly boardsServiceProvider: BoardsServiceProvider; | ||
| @inject(BoardsDataStore) | ||
| private readonly boardsDataStore: BoardsDataStore; | ||
| @inject(HostedPluginSupport) | ||
| private readonly hostedPluginSupport: HostedPluginSupport; | ||
|  | ||
| private readonly toDispose = new DisposableCollection(); | ||
|  | ||
| override onStart(): void { | ||
| this.toDispose.pushAll([ | ||
| this.boardsServiceProvider.onBoardsConfigChanged((config) => | ||
| this.updateBoardsConfig(config) | ||
| ), | ||
| this.sketchServiceClient.onCurrentSketchDidChange((sketch) => | ||
| this.updateSketchPath(sketch) | ||
| ), | ||
| this.configService.onDidChangeDataDirUri((dataDirUri) => | ||
| this.updateDataDirPath(dataDirUri) | ||
| ), | ||
| this.configService.onDidChangeSketchDirUri((userDirUri) => | ||
| this.updateUserDirPath(userDirUri) | ||
| ), | ||
| this.commandService.onDidExecuteCommand(({ commandId, args }) => { | ||
| if ( | ||
| commandId === 'arduino.languageserver.notifyBuildDidComplete' && | ||
| isCompileSummary(args[0]) | ||
| ) { | ||
| this.updateCompileSummary(args[0]); | ||
| } | ||
| }), | ||
| this.boardsDataStore.onChanged((fqbn) => { | ||
| const selectedFqbn = | ||
| this.boardsServiceProvider.boardsConfig.selectedBoard?.fqbn; | ||
| if (selectedFqbn && fqbn.includes(selectedFqbn)) { | ||
| this.updateBoardDetails(selectedFqbn); | ||
| } | ||
| }), | ||
| ]); | ||
| } | ||
|  | ||
| override onReady(): void { | ||
| this.boardsServiceProvider.reconciled.then(() => { | ||
| this.updateBoardsConfig(this.boardsServiceProvider.boardsConfig); | ||
| }); | ||
| this.updateSketchPath(this.sketchServiceClient.tryGetCurrentSketch()); | ||
| this.updateUserDirPath(this.configService.tryGetSketchDirUri()); | ||
| this.updateDataDirPath(this.configService.tryGetDataDirUri()); | ||
| } | ||
|  | ||
| onStop(): void { | ||
| this.toDispose.dispose(); | ||
| } | ||
|  | ||
| private async updateSketchPath( | ||
| sketch: CurrentSketch | undefined | ||
| ): Promise<void> { | ||
| const sketchPath = CurrentSketch.isValid(sketch) | ||
| ? new URI(sketch.uri).path.fsPath() | ||
| : undefined; | ||
| return this.updateState({ key: 'sketchPath', value: sketchPath }); | ||
| } | ||
|  | ||
| private async updateCompileSummary( | ||
| compileSummary: CompileSummary | ||
| ): Promise<void> { | ||
| const apiCompileSummary = toApiCompileSummary(compileSummary); | ||
| return this.updateState({ | ||
| key: 'compileSummary', | ||
| value: apiCompileSummary, | ||
| }); | ||
| } | ||
|  | ||
| private async updateBoardsConfig( | ||
| boardsConfig: BoardsConfig.Config | ||
| ): Promise<void> { | ||
| const fqbn = boardsConfig.selectedBoard?.fqbn; | ||
| const port = boardsConfig.selectedPort; | ||
| await this.updateFqbn(fqbn); | ||
| await this.updateBoardDetails(fqbn); | ||
| await this.updatePort(port); | ||
| } | ||
|  | ||
| private async updateFqbn(fqbn: string | undefined): Promise<void> { | ||
| await this.updateState({ key: 'fqbn', value: fqbn }); | ||
| } | ||
|  | ||
| private async updateBoardDetails(fqbn: string | undefined): Promise<void> { | ||
| const unset = () => | ||
| this.updateState({ key: 'boardDetails', value: undefined }); | ||
| if (!fqbn) { | ||
| return unset(); | ||
| } | ||
| const [details, persistedData] = await Promise.all([ | ||
| this.boardsService.getBoardDetails({ fqbn }), | ||
| this.boardsDataStore.getData(fqbn), | ||
| ]); | ||
| if (!details) { | ||
| return unset(); | ||
| } | ||
| const apiBoardDetails = toApiBoardDetails({ | ||
| ...details, | ||
| configOptions: | ||
| BoardsDataStore.Data.EMPTY === persistedData | ||
| ? details.configOptions | ||
| : persistedData.configOptions.slice(), | ||
| }); | ||
| return this.updateState({ | ||
| key: 'boardDetails', | ||
| value: apiBoardDetails, | ||
| }); | ||
| } | ||
|  | ||
| private async updatePort(port: Port | undefined): Promise<void> { | ||
| const apiPort = port && toApiPort(port); | ||
| return this.updateState({ key: 'port', value: apiPort }); | ||
| } | ||
|  | ||
| private async updateUserDirPath(userDirUri: URI | undefined): Promise<void> { | ||
| const userDirPath = userDirUri?.path.fsPath(); | ||
| return this.updateState({ | ||
| key: 'userDirPath', | ||
| value: userDirPath, | ||
| }); | ||
| } | ||
|  | ||
| private async updateDataDirPath(dataDirUri: URI | undefined): Promise<void> { | ||
| const dataDirPath = dataDirUri?.path.fsPath(); | ||
| return this.updateState({ | ||
| key: 'dataDirPath', | ||
| value: dataDirPath, | ||
| }); | ||
| } | ||
|  | ||
| private async updateState<T extends ArduinoState>( | ||
| params: UpdateStateParams<T> | ||
| ): Promise<void> { | ||
| await this.hostedPluginSupport.didStart; | ||
| return this.commandService.executeCommand( | ||
| 'arduinoAPI.updateState', | ||
| params | ||
| ); | ||
| } | ||
| } | 
  
    
      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
    
  
  
    
              
        
          
          
            38 changes: 38 additions & 0 deletions
          
          38 
        
  arduino-ide-extension/src/browser/theia/terminal/terminal-frontend-contribution.ts
  
  
      
      
   
        
      
      
    
  
    
      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,38 @@ | ||
| import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar'; | ||
| import { CommandRegistry } from '@theia/core/lib/common/command'; | ||
| import { Widget } from '@theia/core/shared/@phosphor/widgets'; | ||
| import { injectable } from '@theia/core/shared/inversify'; | ||
| import { TerminalWidget } from '@theia/terminal/lib/browser/base/terminal-widget'; | ||
| import { | ||
| TerminalCommands, | ||
| TerminalFrontendContribution as TheiaTerminalFrontendContribution, | ||
| } from '@theia/terminal/lib/browser/terminal-frontend-contribution'; | ||
|  | ||
| // Patch for https://github.com/eclipse-theia/theia/pull/12626 | ||
| @injectable() | ||
| export class TerminalFrontendContribution extends TheiaTerminalFrontendContribution { | ||
| override registerCommands(commands: CommandRegistry): void { | ||
| super.registerCommands(commands); | ||
| commands.unregisterCommand(TerminalCommands.SPLIT); | ||
| commands.registerCommand(TerminalCommands.SPLIT, { | ||
| execute: () => this.splitTerminal(), | ||
| isEnabled: (w) => this.withWidget(w, () => true), | ||
| isVisible: (w) => this.withWidget(w, () => true), | ||
| }); | ||
| } | ||
|  | ||
| override registerToolbarItems(toolbar: TabBarToolbarRegistry): void { | ||
| super.registerToolbarItems(toolbar); | ||
| toolbar.unregisterItem(TerminalCommands.SPLIT.id); | ||
| } | ||
|  | ||
| private withWidget<T>( | ||
| widget: Widget | undefined, | ||
| fn: (widget: TerminalWidget) => T | ||
| ): T | false { | ||
| if (widget instanceof TerminalWidget) { | ||
| return fn(widget); | ||
| } | ||
| return false; | ||
| } | ||
| } | 
        
          
          
            23 changes: 23 additions & 0 deletions
          
          23 
        
  arduino-ide-extension/src/browser/theia/terminal/terminal-widget-impl.ts
  
  
      
      
   
        
      
      
    
  
    
      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,23 @@ | ||
| import { injectable } from '@theia/core/shared/inversify'; | ||
| import { TerminalWidgetImpl as TheiaTerminalWidgetImpl } from '@theia/terminal/lib/browser/terminal-widget-impl'; | ||
| import debounce from 'p-debounce'; | ||
|  | ||
| // Patch for https://github.com/eclipse-theia/theia/pull/12587 | ||
| @injectable() | ||
| export class TerminalWidgetImpl extends TheiaTerminalWidgetImpl { | ||
| private readonly debouncedResizeTerminal = debounce( | ||
| () => this.doResizeTerminal(), | ||
| 50 | ||
| ); | ||
|  | ||
| protected override resizeTerminal(): void { | ||
| this.debouncedResizeTerminal(); | ||
| } | ||
|  | ||
| private doResizeTerminal(): void { | ||
| const geo = this.fitAddon.proposeDimensions(); | ||
| const cols = geo.cols; | ||
| const rows = geo.rows - 1; // subtract one row for margin | ||
| this.term.resize(cols, rows); | ||
| } | ||
| } | ||
      
      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.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed that in most files in the project we're using
lodash.debounce, while only in one other file than this one we are usingp-debounce. Maybe it makes sense to stick to one of them and remove a dependency from the project.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for noticing it; I did not. It's because the change is the "cherry-pick" of the upstream fix, which uses
p-debounce. See here: eclipse-theia/theia@6ab16d5#diff-da3ea75041150eb2403247c5ed84542210774c326c0e3b334029f4e3afb51fc0R45.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related: eclipse-theia/theia#9139