Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert text into view in sublime 3 api

I have 4 view in my sublime now. And I want to insert some text to one view. I am trying like this. but no luck.

getAllViews = self.window.views()
jobView = getAllViews[1]
jobEdit = jobView.begin_edit()
jobView.insert(jobEdit, 0, 'Hello')
jobView.end_edit(jobEdit)

Is there any better Idea to do this ?

Updating my question

I am editing my current view layout to 4 pane layout and I want to put some diff data to my newly created layouts. I have this code now .

import sublime
import sublime_plugin
import os, subprocess

class SpliterCommand(sublime_plugin.TextCommand):
    def on_done(self, Regex):
        self.window.set_layout({
            "cols": [0, 0.5, 1],
            "rows": [0.0, 0.33, 0.66, 1.0],
            "cells": [ [0, 0, 1, 3], [1, 0, 2, 1], [1, 1, 2, 2], [1, 2, 2, 3]]
                })

    def run(self, edit):
        self.editview = edit
                self.window = sublime.active_window()
        self.window.show_input_panel('User Input', "Hello",self.on_done,None,None)
        getAllViews = self.window.layouts()

This will split ui in 4 layout. But not able to set data to the new layouts.

like image 238
Achayan Avatar asked Oct 31 '22 03:10

Achayan


1 Answers

The problem is that when you create a new group the group is empty (doesn't contains views), so you can't insert text if there is no view. You need to create a new view in every empty group to insert characters in them. I've updated your on_done method so that a view is created in every empty group and some text is inserted in the new view. This is explained inside code comments.

def on_done(self, Regex):
    self.window.set_layout({
        "cols": [0, 0.5, 1],
        "rows": [0.0, 0.33, 0.66, 1.0],
        "cells": [ [0, 0, 1, 3], [1, 0, 2, 1], [1, 1, 2, 2], [1, 2, 2, 3]]
            })
    # For each of the new groups call putHello (self.window.num_groups() = 4)
    for numGroup in range(self.window.num_groups()):
        # If the group is empty (has no views) then we create a new file (view) and insert the text hello
        if len(self.window.views_in_group(numGroup)) == 0:
            self.window.focus_group(numGroup) # Focus in group
            createdView = self.window.new_file() # New view in group
            createdView.run_command("insert",{"characters": "Hello"}) # Insert in created view

Before:

Before

After:

After

like image 134
sergioFC Avatar answered Nov 15 '22 08:11

sergioFC